0

I have done an append using the following code, where menuElement is a id of a div and appendStr ="<span class="bubbleClass"></span>";

$(menuElement).append(appendStr);

However, when i try $(menuElement+ ".bubbleClass").css("background-color", "red"); it doesnt work.

I want the specific div id stored in menuElement variable to be reflected.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
misthacoder
  • 127
  • 1
  • 1
  • 11
  • found this question, basically the same question i think. http://stackoverflow.com/questions/197748/how-do-i-change-the-background-color-with-javascript – nCore Mar 08 '14 at 23:11
  • @nCore Completely different question, because this issue is about selecting the right element, not changing its colour. – Niet the Dark Absol Mar 08 '14 at 23:12

3 Answers3

0

Since you have dynamically added the item to the DOM, you need to use .find() to access it:

$(menuElement).append(appendStr).find('.bubbleClass').css({
    backgroundColor:'red',
    display:'inline'
});

That is an example based on the limited code you provided, but the use of .find() is the key.

Edited to include the display CSS you said you needed to add as well.

PlantTheIdea
  • 16,061
  • 5
  • 35
  • 40
0

The reason your code is failing is because your selector looks like this: #someID.bubbleClass

In other words, it's looking for the bubbleClass class name on the menuElement itself, not on its children.

Add a space before the . and it should work just fine.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

menuElement should be a jQuery selector like "#menuElement" because it's wrapped inside the jQuery object.

$("#menuElement + .bubbleClass").css("background-color","red");

Cedric Ipkiss
  • 5,662
  • 2
  • 43
  • 72
  • Yes i had the menuElement variable to hold a string value which is actually a id of a element for example menuElement="#message". @PlantTheIdea user's solution worked for me. – misthacoder Mar 08 '14 at 23:26