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".
To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox 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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With