1

What I would like is to do this:

var theId = anId;

$('#someId .someClass[id=' + theId + ']').css('border-color', 'red');

...but using a varible for the "selector part":

var mySelector;

if(condition1){mySelector= $('#someId .someClass');}
if(condition2){mySelector= $('#anotherId .anotherClass');}

$.each(anArray, function(i, v) {
    mySelector[id=' + v.id + '].css('border-color', 'red'); 
});

(This is unfortunately not working. I need help for correcting this syntax)

I tried several syntax that did not work. Hope someone can help. Thank you in advance for your replies. Cheers. Marc.

5
  • I don't undertand, you want a child of .someClass that has an ID of theId? Commented Mar 6, 2012 at 10:43
  • This question is pretty confusing. Selectors are nothing else than strings and $() does not return a selector. Commented Mar 6, 2012 at 10:45
  • It's not clear what you want. Your first sample should work. Your second sample is unclear to me. Commented Mar 6, 2012 at 10:45
  • 3
    Can't you just use $('#' + theId)? Commented Mar 6, 2012 at 10:46
  • Hello everyone. Thanks for trying to help. I modified what I am trying to do but which is not working. I edited my post. Have a check... Commented Mar 6, 2012 at 10:59

4 Answers 4

1

After your edit, I think you should do this:

var mySelector_string;
var theId = anId;

if(condition1){mySelector_string= '#someId .someClass';}
if(condition2){mySelector_string= '#anotherId .anotherClass';}

$.each(anArray, function(i, v) {
    $(mySelector_string+"[id='" + v.id + "']").css('border-color', 'red'); 
});
Sign up to request clarification or add additional context in comments.

3 Comments

Why is return false; in there?
Efficiency; when the condition is met, we exit from the cycle.
Hello DotMat. This is not what I am looking for. Proberly because I was not clear in my post. I just edited it. You can check. It will be more clear to you what I am tring to do now...
0
$('#someId .someClass #'+theId).css('border-color', 'red');

will get a child of .someClass with id theId

$('#someId .someClass#'+theId).css('border-color', 'red');

will get an instance of .someClass with id theId

1 Comment

Hello Mild Fuzz. This is not what I am looking for. Proberly because I was not clear in my post. I just edited it. You can check. It will be more clear to you what I am tring to do now...
0

If your question is how you can cache a selector which is dynamically constructed for re-use, then you can do the following:

$("div p a " + someOtherSelectorRequirement).click(function(){ });

can become

var a = $("div p a " + someOtherSelectorRequirement);
a.each(function(){ Console.Log($(this).text());
a.addClass("someClass");

if you are asking if you can re-use the same selector in an object, or variable.

1 Comment

Hello SpaceBison. This is not what I am looking for. Proberly because I was not clear in my post. I just edited it. You can check. It will be more clear to you what I am tring to do now...
0

I think there're a couple of misused terms in your question. Learning them will probably help you to fix your code.

jQuery object instance: it's a JavaScript object that (among other things) contains a possibly empty internal array of DOM nodes on which you can perform further operations. You obtain one every time you call jQuery() (possibly aliased as $()).

Selector: It's a JavaScript string that represents a CSS query rule, e.g.: "ul>li:first". Many functions in jQuery, including the jQuery() function, accept selectors as parameters and use them to find or filter DOM nodes in the current chain.

So you basically have strings and objects and both are regular JavaScript data types and behave as such. You can, for instance, store them in variables or pass them as function arguments. You just need to know the difference between a CSS selector and the HTML elements found through it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.