Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Here is my code:

hasAttr = new function(tag, attrName) {
    return (tag.attrName)
}

Will this code work? I can't test it now because I cannot acces jsfiddle and jsbin but Down For Everyone or Just Me? says something else.

  1. jsFiddle Down For Everyone or Just Me
  2. jsBin Down For Everyone or Just Me

Example usage:

JavaScript:

alert(hasAttr("div", "id"))

HTML:

<div id="header"></div>

Expected:

(In alert box)
    true
                OK

Can someone test it for me?

If it doesn't work, how should I code it?

I want hasAttr to return true or false.

I will be open to code optimization, cleaner code etc.

Thanks in advance!

share|improve this question
1  
hasAttr("div", "id") You are sending a string "div" and you are treating it like an object in your function. Something is wrong! –  epascarello Mar 19 '12 at 0:10

2 Answers 2

up vote 6 down vote accepted

To check if an HTML element has an attribute, you should use hasAttribute. If you have to support IE6 and IE7, check its existence using [name].

Given that, the implementation of hasAttr would be something like this:

hasAttr = function(tag, attrName) {
    return tag.hasAttribute ? tag.hasAttribute(attrName)
                            : tag[attrName] !== undefined;
}
share|improve this answer
    
Can you rewrite using if instead of ?/: please? –  Anish Gupta Mar 19 '12 at 11:44
3  
return a ? b : c means if(a) { return b; } else { return c; }. –  Quentin Pradet Mar 19 '12 at 12:41
    
Why the new here? –  Benjamin Gruenbaum Sep 11 '13 at 21:34
    
@BenjaminGruenbaum: Good catch, I don't think it belongs there. It was in the original code but looks like a bug, so I edited it out of my post. –  seand Sep 12 '13 at 2:15
    
!!tag[attrName] will return false if the attribute exists but is an empty string... –  Brett Zamir Oct 11 '13 at 19:58

Testing JavaScript code in jsFiddle to make sure it works is suicide. The difficult part about JavaScript is really cross-browser compatibility, which means you just can't just test in one browser to say it works.

That being said, the name hasAttr makes me think that you want to return true/false, which is not what you're doing. You should use DOM's element.hasAttribute(), which already implements what you're trying to do. It's DOM 1, so it's safe to use and will work in any browser supporting JavaScript.

By the way, why are you using this funky syntax to create your function? Is there a good reason not to use function hasAttribute() { ... }?

(You should accept seand's answer, though, I didn't know it wasn't supported until IE 8. You should also consider using something like jQuery which will help you avoid mistakes like that.)

share|improve this answer
    
you beat me by ~20 seconds :) –  seand Mar 18 '12 at 21:55
    
Not really the same answer! I didn't know about IE, and you didn't notice he wants to see "true", which means he simply wants hasAttribute. At least I think so. –  Quentin Pradet Mar 18 '12 at 21:59
    
Ah, good catch, thanks... changed my answer. –  seand Mar 18 '12 at 22:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.