1

I'm trying to change the HTML of the following using jQuery

'<label for="aTitle" generated="true" class="error aTitle" style=""></label>';

But if I use the jQuery code below nothing changes. I cant remove the space there. Any other method that'll make this work?

$(".error aTitle").html("Hello <b>world!</b>");
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Norman
  • 6,159
  • 23
  • 88
  • 141

3 Answers3

9

Remove the space and add . for class selector aTitle

$(".error.aTitle").html("Hello <b>world!</b>");
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
0

Your current selector $(".error aTitle") says to match all <aTitle> elements whose parent element has the CSS class .error, which isn't quite what you want.

To select the elements you want, you need to change your selector to $('.error.aTitle'). Every class has to be preceeded by a period (to denote a CSS class), otherwise it'll be seen as an element (ie a <div>). Spaces between items in a selector indicate heritage, so using a selector like $('.error .aTitle') means you want to select all elements with a CSS class of .aTitle whose parent element has the CSS class .error.

Another, further example of a selector you can use (which may be a bit of overkill) is $('label.aTitle.error'), which says to select all <label> with the CSS classes of .aTitle and .error. This selector would not match, say, <input type="text" class="error aTitle" />.

saluce
  • 13,035
  • 3
  • 50
  • 67
0
class="error aTitle"

You are actually setting two classes here, not one. The element has the class error AND it has the class aTitle.

You want to select the element(s) that have both classes:

$('.error.aTitle')
gen_Eric
  • 223,194
  • 41
  • 299
  • 337