1
\$\begingroup\$

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!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ hasAttr("div", "id") You are sending a string "div" and you are treating it like an object in your function. Something is wrong! \$\endgroup\$ Commented Mar 19, 2012 at 0:10

2 Answers 2

6
\$\begingroup\$

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;
}
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Can you rewrite using if instead of ?/: please? \$\endgroup\$ Commented Mar 19, 2012 at 11:44
  • 3
    \$\begingroup\$ return a ? b : c means if(a) { return b; } else { return c; }. \$\endgroup\$ Commented Mar 19, 2012 at 12:41
  • \$\begingroup\$ Why the new here? \$\endgroup\$ Commented Sep 11, 2013 at 21:34
  • \$\begingroup\$ @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. \$\endgroup\$
    – seand
    Commented Sep 12, 2013 at 2:15
  • \$\begingroup\$ !!tag[attrName] will return false if the attribute exists but is an empty string... \$\endgroup\$ Commented Oct 11, 2013 at 19:58
2
\$\begingroup\$

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.)

\$\endgroup\$
3
  • \$\begingroup\$ you beat me by ~20 seconds :) \$\endgroup\$
    – seand
    Commented Mar 18, 2012 at 21:55
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 18, 2012 at 21:59
  • \$\begingroup\$ Ah, good catch, thanks... changed my answer. \$\endgroup\$
    – seand
    Commented Mar 18, 2012 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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