Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value for a input button using jquery?

Tags:

jquery

get

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".

like image 594
ssss05 Avatar asked Mar 15 '12 13:03

ssss05


People also ask

How can get input tag value in jQuery?

To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox value.

How do you get a click value?

To get the value of an input on button click in React: Declare a state variable that tracks the value of the input field. Add an onClick prop to a button element.

How do I set the value of a element in jQuery?

JQuery val() method: This method return/set the value attribute of selected elements. If we use this method to return value, it will return the value of the FIRST selected element. If we use this method to set value, it will set one or more than one value attribute for set of selected elements.


1 Answers

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!

like image 100
Richard Dalton Avatar answered Sep 24 '22 01:09

Richard Dalton