The reason they are not the same is because attr('value')
gets the value of the value
attribute directly from the original HTML code, it is not updated with the DOM, meaning if the value of value
is changed after the page has loaded, either by user input (typing into an <input>
element, or via manipulation with JavaScript, these changes will not be reflected in the returned value of .attr()
.
A better way is to use the .val()
method of the jQuery object.
Edit
To get the attribute of the value from a DOM Element (i.e. not returned by the $()
or jQuery()
function) use the element.getAttribute()
method, which is native, you would use it like this:
selectbox.options[selectbox.selectedIndex].getAttribute("value");