2

I have simple button:

<button type="button" id="somebutton" value="start">blah</button>

in jQuery, on the bind('click, function(){.... I have this:

$(this).attr("value")

of course what this does is gets the value of an attribute, which in this particular case is: "start". OK, this works in firefox, I get correct value of "start". However in IE 7 the value I get is "somebutton".

why?

5 Answers 5

4

I know this was posted a while ago, but in case anyone is searching for an answer...

IE reports both the .val() and .attr("value") as being the text label (content) of the button element instead of the actual value of the value attribute.

You can work around it by temporarily removing the button's label:

var label = $(this).text(); 
$(this).text('');
var buttonValue = $(this).val();
$(this).text(label);

There are a few other quirks with buttons in IE. I have posted a fix for most of the issues here.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, this doesn't work if you have content such as an image inside the button
Good point. I guess you'd have check for and remove child nodes as well as text.
3

I have come across that before. I don't know why that happens for <button>. It could be a bug in jQuery, or it could be a bug in IE, or it could be a combination of both.

In the end, I opted to go with a standard <input type="button"/> to get around the issue.

1 Comment

you're right, I tried using <input type="button/> and it passes value correctly now..go figure.
1

Using button.attributes.value.nodeValue should give you the correct value. See jQuery Bug #1954 for details.

Regarding the why in the question: You're hitting one of IE's bugs. In this case jQuery does not yet provide a workaround.

Comments

0

use the <input> tag. It should work

Comments

0

To solve the problem you'll have to lose the <button> tag and go for a <input> tag instead.

Comments

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.