1

I have a div (id =container) that is containing other divs (many classes) I am able to use the container as a string "AAAA" but can't do the same effect with a variable mystring

<div id="container">
<div class="AAAA">AAAA content</div>
<div class="AAAA">AAAA content</div>
<div class="BBBB">BBBB content</div>
<div class="CCCC">CCCC content</div>
<div class="DDDD">DDDD content</div>
<div class="AAAA">AAAA content</div>
</div>


<script>
var myclass = "#container>div:not("+".AAAA"+")"; // works
$(myclass).css("background-color", "red"); //works fine
//var mystring = "AAAA";
//var myclass = "\"#container>div:not(."+mystring+")\"";
//$(myclass).css("background-color", "red"); // doesn't work
</script>

2 Answers 2

5

Remove the escaped quotes :

var myclass = "#container>div:not(."+mystring+")";
Sign up to request clarification or add additional context in comments.

1 Comment

I think you badly tried : this would make the following line exactly the same than the one that worked.
1

Working demo http://jsfiddle.net/xWWcZ/

Reason: you don't need to escape \# hash.

Hope rest fits the cause.

Code

//var myclass = "#container>div:not("+".AAAA"+")"; // works
//$(myclass).css("background-color", "red"); //works fine
var mystring = "AAAA";
var myclass = "#container>div:not(."+mystring+")";
$(myclass).css("background-color", "red"); // work now​​​

1 Comment

thanks guys, I really went over this again and again, I did that before but did not refresh Firefox and thought it was something with my code. now that I saw that i works under jsfiddle.net I was sure the problem was as easy as pressing f5. thanks again

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.