Skip to content
mnaoumov.dev
Go back

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!


Share this post on:

Previous Post
Debugging weird focus behavior in browser
Next Post
Execution of external commands in PowerShell done right