views:

1201

answers:

4

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?

+3  A: 

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.

ern
you're right, I tried using <input type="button/> and it passes value correctly now..go figure.
ra170
A: 
To solve the problem 

you'll have to loose the <button> tag and go for a <input> tag instead:
Gordian Yuan
A: 

use the < input > tag. it should def work

A Hassan
+3  A: 

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.

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