Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - Click Submit Button Get Form Value

I have the following function and all i am trying to do is get the value out of the form field.

$( ".searchbutton" ).click(function() {
    var tc = $(this).closest("form input[name='searchbox']").val();
    alert(tc);      
    return false;
}); 

The alert keeps telling me "Undefined". I have treid closest, parent, parents, find, etc. I don't know what im doing wrong. Im clicking the submit button and all i want in return is the value in the search box. Please help.

html

<form action="/index.php" method="get" class="qsearch" >
<input type="text" id="fsearch" name="searchbox" >
<input class="searchbutton" type="submit" value="Submit">
</form>
like image 429
user982853 Avatar asked Dec 26 '22 00:12

user982853


1 Answers

Try this:

$( ".searchbutton" ).click(function() {
    var tc = $(this).closest("form").find("input[name='searchbox']").val();
    alert(tc);      
    return false;
}); 

Update Yep, it work with your HTML - see here http://jsfiddle.net/qa6z3n1b/

As alternative - you must use

$( ".searchbutton" ).click(function() {
    var tc = $(this).siblings("input[name='searchbox']").val();
    alert(tc);      
    return false;
}); 

in your case. http://jsfiddle.net/qa6z3n1b/1/

like image 176
Vladimir Chichi Avatar answered Jan 04 '23 23:01

Vladimir Chichi