0

I am trying to find a cross browser compatible way to find if a object has a certain property or not.

Eg. span

var elem = document.getElementById('span1');

if(elem.hasOwnProperty('title'))
{
}

this works in IE9 but throws nasty errors on IE8 and whole site is brought down just by this line. Is there any browser compatible way to find if object has certain property or not?

2
  • 1
    HTML attribute != DOM property Commented Jun 22, 2012 at 1:50
  • To clarify, you are talking specifically about DOM objects? And when you say "property", do you mean "property" or "attribute"? Commented Jun 22, 2012 at 1:55

1 Answer 1

2

In this case, it's an HTML element so it would have a title attribute.

if(elem.getAttribute("title")) {...}
Sign up to request clarification or add additional context in comments.

4 Comments

I certainly hope so, given I've used it on every project I've ever made.
@antisanity In this case, the only possible falsy value is an empty string, which you probably wouldn't want to do anything with anyway. Although it is something to look out for.
"0" is truthy in JavaScript.
Since OP listed a jQuery tag should probably mention $(..).attr(). While getAttribute is pretty safe there are some cases where it won't work at all, e.g class in ie6.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.