Search code examples
jqueryget

How to get value for a input button using jquery?


I need to retrieve a value for a clickable button using jquery. I have 3 buttons under a <div>:

<div class="button1">
        <input type="button" value="one" id="One" onclick= "location.href='index.html'"/> 
        <input type="button" value="two" id="Two" onclick="location.href='index.html'" />
        <input type="button" value="three" id="Three" onclick="location.href='index.html'" />
 </div> 

If I click on button 1, I need to get value for a button 1. I don't know, how exactly get the value of this.

Here is my js:

$(document).ready(function() {
    $('.button1').click(function() {
        var text = $(this).text();
        $("input").val(text);
        });
});

I know this script I wrote is wrong, because I am getting values as "one,two,three".


Solution

  • Like any other input, to get the value of a button use .val():

    $('input:button').click(function() {
        alert($(this).val());
    });​
    

    http://jsfiddle.net/3QDM5/

    I'm not sure exactly what you're trying to do with your script though!