2
<form id="Task1_1_1">
    Keywords: <input type="text" name="twitKey" value="iphone"><br><br> 
    Coordinates: <input type="text" name="twitLoc" value=""><br> <br> 
    Scope: <input type="text" name="scope" value="10" > km<br> <br>
    <button id="sendButton1_1_1">Search</button>
</form>

This is the form. I can get the values of the form through the following code.

$('#sendButton1_1_1').on('click', function(e) {
    console.log($('#Task1_1_1').val());
});

Here is the output from the js console

twitKey=iphone&twitLoc=&scope=10

My question is how to get the value of twitKey by using selectors? Or do I have to parse myself? I already know I get value by id Get the value from HTML form using jquery

I want to know whether I can get the value from value name such as

name="twitKey"
1
  • 1
    input[name='twitkey'] Commented May 13, 2015 at 4:03

2 Answers 2

3

Try with -

console.log($('form#Task1_1_1 input[name=twitKey]').val());

Add a class/id to it -

<input type="text" name="twitKey" id="twitKey" value="iphone" >

And JS - console.log($('#twitKey').val());

Sign up to request clarification or add additional context in comments.

1 Comment

If I use the first way, Is there any method that can ensure input[name=twitKey] come from the form with id"Task1_1_1"?
2

Here is a snippet with multiple ways to grab the value of twitKey:

$('input[name="twitKey"]').val(); //iphone
$('input:first').val(); //iphone
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="Task1_1_1">
    Keywords: <input type="text" name="twitKey" value="iphone"><br><br> 
    Coordinates: <input type="text" name="twitLoc" value=""><br> <br> 
    Scope: <input type="text" name="scope" value="10" > km<br> <br>
    <button id="sendButton1_1_1">Search</button>
</form>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.