IE vs Firefox and Chrome and jQuery val() vs attr(‘value’)

Hi folks

I am currently fixing one bug at work and found an interesting problem.

Something works in IE but doesn’t work on Firefox and Chrome.

After some debugging I found the problem

<span id="someSpan" value="some value">some caption</span>

And it is used in JavaScript

var value = $('#someSpan').val();

This works only in IE. In Firefox and Chrome it returns null.

According to documentation jQuery .val() function makes sense mostly for form elements and it just checks elem.value property, but this property is set only in IE.

So the fix is

var value = $('#someSpan').attr('value');

Gotcha!

Stay tuned!

Advertisements

About mnaoumov

Senior .NET Developer in Readify
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to IE vs Firefox and Chrome and jQuery val() vs attr(‘value’)

  1. I’m assuming this is because `val()` is similar to `prop(“value”)`not `attr(“value”)`. The difference between `attr` and `prop` gets so many people.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s