3

I need an html structure that looks like this:

<div id="ABTestBanner_New">
</div>

<div id="ABTestBanner_Fav">
</div>

<div id="ABTestBanner_Budget">
</div>

I need to use it in an AB split test. So I need to write in javaScript, and I need all three divs to be unique id's but have the same CSS. I tried to do it this way but only the #ABTestBanner_Budget is getting the CSS applied:

var $divNew = $('<div></div>', {'id': 'ABTestBanner_New'});
$('div.samplesCell.samples1').after($divNew)

var $divFav = $('<div></div>', {'id': 'ABTestBanner_Fav'});
$($divNew).after($divFav);

var $divBudget = $('<div></div>', {'id': 'ABTestBanner_Budget'});
$($divFav).after($divBudget);

$('#ABTestBanner_New' && '#ABTestBanner_Fav' && '#ABTestBanner_Budget').css({
    float : 'left',
    height : '250px',
    width : '950px',
    backgroundColor:'#0080FF',
    margin:'20px 0px 20px 0px'
});

Any thoughts on what I am doing wrong?

4
  • 1
    Seperate them with commas. Commented Feb 19, 2014 at 19:54
  • basic javascript: 'a' && 'b' && 'c' is not doing what you think it's doing... Commented Feb 19, 2014 at 19:56
  • You might as well have written the following: $(true).css({ blah, blah }); ... just set a bookmark for the API documentation (seen in my previous comment). Any valid string will evaluation to true ... especially when evaluating a function argument with ampersands performing boolean logic. Commented Feb 19, 2014 at 20:08
  • 1
    Use a class? Your CSS is not dynamic in any way. Commented Feb 19, 2014 at 20:10

4 Answers 4

11

See jquery docs here: http://api.jquery.com/category/selectors/

$('#ABTestBanner_New, #ABTestBanner_Fav, #ABTestBanner_Budget').css({
    /*your style*/
});
Sign up to request clarification or add additional context in comments.

2 Comments

You should NOT have even typed that out. I added the link to the docs (as a comment to the original question). People should not be asking questions like this expecting to be "spoon fed". Devs have spent a lot of time developing good documentation -- we need to point people to the APIs. If they're confused by what they read there -- that's different. But, they should at least look at the documentation first.
@JoeJohnson, Thank for feedback, I'm duplicate your link to API in my answer.
2

Simply use:

$('div[id^="ABTestBanner"]').css({
    /* styles */
});

DEMO: http://jsfiddle.net/rw53Q/

The div[id^="ABTestBanner"] selector targets all div elements whose id start with ABTestBanner.

Comments

1

Why don't you just add a class to those elements and style the class the way you want to?

$ABTestBanner_New.addClass("foo");
$ABTestBanner_Fav.addClass("foo");
$ABTestBanner_Budget.addClass("foo");

Adding/Removing a class is a little more efficient than adding inline styles on the fly, it also helps to keep all your styles in the CSS file where they belong. :-)

Comments

0
$('#ABTestBanner_New , #ABTestBanner_Fav , #ABTestBanner_Budget').css({
    float : 'left',
    height : '250px',
    width : '950px',
    backgroundColor:'#0080FF',
    margin:'20px 0px 20px 0px'
});

instead of && use comma

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.