tags:

views:

6961

answers:

7

A simple one, I'm trying to retrieve the value attribute of a button when its been pressed using jQuery, here's what I have:

<script type="text/javascript">
    $(document).ready(function() {
        $('.my_button').click(function() {
            alert($(this).val());
        });
    });
</script>

<button class="my_button" name="buttonName" value="buttonValue">
    Button Label</button>

In Firefox my alert displays 'buttonValue' which is great but in IE7 it displays 'Button Label'.

What jQuery should I use to always get the button's value? Or should I be using a different approach?

Many thanks.

ANSWER: I'm now using

<input class="my_button" type="image" src="whatever.png" value="buttonValue" />
+5  A: 

As a button value is an attribute you need to use the .attr() method in jquery. This should do it

<script type="text/javascript">
    $(document).ready(function() {
     $('.my_button').click(function() {
      alert($(this).attr("value"));
     });
    });
</script>

You can also use attr to set attributes, more info in the docs

Neil Aitken
Thanks for that, IE7 is still returning 'Button Label' though.
Simon Hutton
IE7 is having difficutly with the fact you have text data inside the element. if you want to use an image you should use <input type="image" src="whatever.png" />
Neil Aitken
IE6 and IE7 (not IE8) seem to exhibit a bug related to <button> tags. The inner text of the tag overwrites the value attribute to almost all effects.
Álvaro G. Vicario
A: 

if you want to get the value attribute (buttonValue) then I'd use:

<script type="text/javascript">
    $(document).ready(function() {
        $('.my_button').click(function() {
            alert($(this).attr('value'));
        });
    });
</script>
Wayne Austin
But it still does not work with IE7, see my work-around post below.
sbglasius
+3  A: 

try this for your button:

<input type="button" class="my_button" name="buttonName" value="buttonValue" />
Natrium
Thanks but I'm actually displaying an image in my button so it needs to be a <button>.
Simon Hutton
Ok, realised I can use and input as type="image" which works, thanks.
Simon Hutton
+8  A: 

I know this was posted a while ago, but in case anyone is searching for an answer and really wants to use a button element instead of an input element...

You can not use .attr('value') or .val() with a button in IE. 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 the two most common issues here.

postpostmodern
This question is the top google search result for 'jquery button value ie7', exactly what I needed to know and a great solution. Thanks
Marc Roberts
Great tip, thanks!
Dai Bok
A: 

Thanks postpostmodern.. Worked a treat

burlistic
A: 

Thanks postpostmodern... you also worked me a treat. :)

James
+1  A: 

Inspired by postpostmodern I have made this, to make .val() work throughout my javascript code:

jQuery(function($) {

    if($.browser.msie) {
        // Fixes a know issue, that buttons value is overwritten with the text

        // Someone with more jQuery experience can probably tell me
        // how not to polute jQuery.fn here:
        jQuery.fn._orig_val = jQuery.fn.val

        jQuery.fn.val = function(value) {
            var elem = $(this);
            var html
            if(elem.attr('type') == 'button') {
                // if button, hide button text while getting val()
                html = elem.html()
                elem.html('')
            }
            // Use original function
            var result = elem._orig_val(value);
            if(elem.attr('type') == 'button') {
                elem.html(html)
            }
            return result;
        }
    }
})

It does however, not solve the submit problem, solved by postpostmodern. Perhaps this could be included in postpostmodern's solution here: http://gist.github.com/251287

sbglasius