views:

474

answers:

1

Hi, I m new to JQUery..If i checked the checkBox as Required ,i need to get a * to a div.

    <label class='Label3'>Options
    <input id='option1' class='checkbox' type='checkbox'></input><label class='choice' for='checkbox'>Required</label>
    <input id='option2' class='checkbox' type='checkbox'></input><label class='choice' for='checkbox'>No duplicates</label>
    </label>

If i selected the option1 then i need to get a * Attached to my Div that is already generated before this..How can i do this ..Please Suggest me....

+2  A: 

All you should need is the onclick event.

$('#option1').click(function() {
  if (this.checked) {
     $('#targetDiv').text('*');
  } else {
     $('#targetDiv').text('');
  }
});

See this example.

altCognito
I assume you meant an empty string in the 'else' branch...
Visage
I need to attach a * to the top of the Div
Aruna
I sure did, and the this should have been slightly different as well. Posting the example. (Hadn't tested it Yet)
altCognito
In the example I use a span. A span makes more sense than a div, because you probably want it to be inline with the text of the question.
altCognito
If you want the div where it appends the *, then look at this example: http://jsbin.com/ataje The downside to this approach is you'll have to insure that the state of the * in the div is consistent with the checkbox. The span approach always works because it's wholly replacing the state of the span whereas the div makes an assumption that when it adds the star, it didn't have a star already (say at page load). Sure, you could add a check, but seems like a lot more complicated solution than the span.
altCognito