jQuery: The Write Less, Do More JavaScript Library

passing a variable through an attribute filter

View: New views
4 Messages — Rating Filter:   Alert me  

passing a variable through an attribute filter

by barbercraig :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Guys,

This is my very first post! so please be kind.... :)

Ok, i am very novice to jQuery. But what i am trying to do is use the jQuery attribute selector to select an input with a specific name.

I can select an input of name type fine by doing the folowing:

      $("input[name='name']").do something...

But, can i pass a variable into that filter? to search for a variable?

So if i had a variable called 'field_name' containing the text: 'email', can i do something like this:

     $("input[name='(field_name)']").do something...

So that it in this caseit would find an input with the name 'email'

Sorry if this is a doofus question, just can't find any examples of this anywhere

thanks for your help guys,

cheers

Craig

Re: passing a variable through an attribute filter

by Marcel Araujo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, you can do this!!!

Sorry for my poor english but I hope help you!

$("input[name='txtName']").css('background-color','#F00');

And this way too.

var field_property = "txtName";

$("input[name='+field_property+']").css('background-color','#F00');



2009/11/3 barbercraig <craigbarber75@...>

Hi Guys,

This is my very first post! so please be kind.... :)

Ok, i am very novice to jQuery. But what i am trying to do is use the jQuery
attribute selector to select an input with a specific name.

I can select an input of name type fine by doing the folowing:

     $("input[name='name']").do something...

But, can i pass a variable into that filter? to search for a variable?

So if i had a variable called 'field_name' containing the text: 'email', can
i do something like this:

    $("input[name='(field_name)']").do something...

So that it in this caseit would find an input with the name 'email'

Sorry if this is a doofus question, just can't find any examples of this
anywhere

thanks for your help guys,

cheers

Craig

--
View this message in context: http://old.nabble.com/passing-a-variable-through-an-attribute-filter-tp26160102s27240p26160102.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.




--
Abraços......

Marcel Araujo
System Analyst
Developer Java/PHP/RIA
Linux User #490101

Re: passing a variable through an attribute filter

by Michel Belleville :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Let's see :

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

So, what is that exactly ?
  • $() is a method
  • "input[name='name']" is the only argument of $() ; oh my gosh, it's a string !
So I guess I can do this : $("input[name='" + "name" + "']") and... My oh my it still works !
Well, let's try something a little bit more acrobatic :
var field_name = "name";
$("input[name='" + field_name + "']")
Awesome.

Hope it helps.

Michel Belleville


2009/11/3 barbercraig <craigbarber75@...>

Hi Guys,

This is my very first post! so please be kind.... :)

Ok, i am very novice to jQuery. But what i am trying to do is use the jQuery
attribute selector to select an input with a specific name.

I can select an input of name type fine by doing the folowing:

     $("input[name='name']").do something...

But, can i pass a variable into that filter? to search for a variable?

So if i had a variable called 'field_name' containing the text: 'email', can
i do something like this:

    $("input[name='(field_name)']").do something...

So that it in this caseit would find an input with the name 'email'

Sorry if this is a doofus question, just can't find any examples of this
anywhere

thanks for your help guys,

cheers

Craig

--
View this message in context: http://old.nabble.com/passing-a-variable-through-an-attribute-filter-tp26160102s27240p26160102.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.



Re: passing a variable through an attribute filter

by MorningZ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Craig... just keep one major thought in regards to selectors.... *
it's just a string *

and whether you hard code it in there or build it up like the post
above demonstrates, in the end you are just passing a string to the
selector engine......