sasori
sasori

Reputation: 5463

Proper jquery selector choices

I have this part of my form:

<input name="myName" type="text" value="myValue">

My questions are:

Upvotes: 1

Views: 100

Answers (9)

Acil Az
Acil Az

Reputation: 168

It's simple like CSS to read the textfield parameter.

use the jquery command to get their values..

here's my example :

$('object_parameter').val();



easier.

Upvotes: 0

musefan
musefan

Reputation: 48455

Here is one option...

var value = $("[name=''.$id.'']").attr("value");

Example Here

To work with external JS file...

in JS file:

var knownID;

function SetID(id){
   knownID = id;
}

function UsingValue(){
   var value = $("[name='" + knownID + "']").attr("value");
}

in PHP page (javascript function on page load):

SetID(.$id.); //not sure the correct syntax for PHP rendering

Though I would just prefer to do my JavaScript on the PHP page, if I need some heavy functions I would pass the element to the JS file to be used

Upvotes: 0

T9b
T9b

Reputation: 3502

What you have written is a bit odd. Are you also using php as well? Why the dots, inverted commas, and the dollar sign as your id and value?

In any event

<input id="id" name="id" type="text" value="textvalue"> 

you should use the .val() function.

$('#id').val();

Upvotes: 0

mdm
mdm

Reputation: 12630

Probably something like:

$('input[name="name-of-input"]')

And then you can extract the value using:

var v = $('input[name="name-of-input"]').val();

If you want to get a list of names of input elements on your page then you'll need to add a bit more, e.g. to enumerate the tags and output their names:

$('input[type="text"]').each(function (idx, elem) { 
    alert($(elem).attr('name')); 
});

You could make this more specific by adding a class attribute to the inputs you want to examine (in it's current form the code above will look at ALL textboxes on the page).

Upvotes: 1

T9b
T9b

Reputation: 3502

If @mdm is correct (and I think he is) your question is not sufficiently detailed to give the full answer.

If there is some event triggered such as an onfocus which you want to trigger an action than you could conceivably use the this.id selector to dynamically get your element's ID but because you have not posted the full code it is not possble to show you how this would work.

Upvotes: 0

YuS
YuS

Reputation: 2045

Do you mean like this?

$('input[name="input_name_here"]')

Upvotes: 0

Paul Alan Taylor
Paul Alan Taylor

Reputation: 10680

You'll need to use attribute selection.

e.g.

$('input[name="NameYouAreLookingFor"]');

http://api.jquery.com/attribute-equals-selector/

Upvotes: 0

Anthony Grist
Anthony Grist

Reputation: 38345

Use the attribute equals selector like so: $('input[name="name"]').

Upvotes: 0

Alex Pliutau
Alex Pliutau

Reputation: 21947

You can use selector by attribute:

$('input[name="myname"]');

Upvotes: 0

Related Questions