views:

135

answers:

3

Seems like a simple thing to do (although im still learning the ins and outs of jQuery).

Here's my HTML:

<div id="fbsignup">
    <div class="message messageerror"> 
       <strong>There were errors with your registration:</strong> 
          <ul></ul> 
    </div> 
</div>

Here's my (attempted) jQuery:

// Check Required Fields.
$('#fbsignup input.required').each(function () {
   if ($(this).val().trim() == '') {
      $(this).next('em').html('*').show();
      $('#fbsignup .message ul').add('li').html('Fields marked with * are required.');
    }
});

And this is what it is doing to the DOM:

<div class="message messageerror"> 
   <strong>There were errors with your registration:</strong> 
      <ul>Fields marked with * are required.</ul> 
</div> 

It's adding the text to the inner html. I want it to add a <li> element with the inner html of what i set - i thought this is how chaining works?

This is the HTML i want to end up with:

<div class="message messageerror"> 
   <strong>There were errors with your registration:</strong> 
      <ul>
         <li>Fields marked with * are required.</li> 
      </ul>
</div>

I also tried this:

$('#fbsignup .message ul').add('<li>Fields marked with * are required.</li>');

Which does absolutely nothing.

What am i missing? Is the .add function not the right thing to use here?

A: 

The problem for me seems to be that you are using a function called .add() and I don't think there is such function -wrong.
Likely you can use .append() to do your will http://api.jquery.com/?ns0=1&amp;s=append =)

A good general rule for me is to try stuff beforehand on the Firebug console (over FireFox)

Fabiano PS
.add is certainly a function (http://api.jquery.com/add/). i also tried append, does same thing (adds html, not element with html)
RPM1984
OK - got it working with .append('<li>foo</li>'). shame the parameter is only content and not an actual dom element (e.g .append('li').html('foo'). anyway, all good. thanks
RPM1984
hehe Shame on me, .add is a function, it just that the API search didn't point it out for me :P
Fabiano PS
A: 

Hi, Try

$('#fbsignup .message ul li').add

hameed
selector wont return anything as there is no li element (yet - that is the whole point)
RPM1984
+2  A: 

You want to use .appendTo(...) for this style.

$("<li/>").appendTo("#fbsignup .message ul").html("Fields marked with * are required.");​

Try it.

Strelok
Nice. I like this better because you can chain on selectors (.html). Thanks
RPM1984