tags:

views:

181

answers:

4

How to remove a class that has been appended to a div id? Earlier I appended a star to a label,when a checkbox is click.

 $("<span class='req'><em> * </em></span>").appendTo("#displayPanel #labelstr"+div_id);

Now,if the same checkbox is clicked again I want to remove that star. I know there is a remove() option, but what is the proper code for it? Please guide me.

+1  A: 

My guess:

$('#displayPanel #labelstr'+div_id+' .req').remove();

if you want to remove the .req and only have one of them per element.

Alan
ya you are right. I did it wrongly. Instead using $("#labelstr" + div_id + " span.req").remove(); as suggested by you and everyone else.Thank you.Just one id was enough,so I left out that #labelstr.
Angeline Aarthi
+2  A: 

By the looks of it, you're not trying to remove a class which would be the removeClass() command, but instead need to remove a child element from a <div>

You could do

$("#displayPanel #labelstr" + div_id + " span.req").remove();

to remove the child span element with class 'req' from the <div>.

Russ Cam
Why the downvote? This is exactly what I said, as the OP seems to have misphrased his question.
Alan
+1 :) ... 15 characters
Daniel
@Alan- I haven't downvoted you
Russ Cam
ya you are right. I did it wrongly. Instead using $("#labelstr" + div_id + " span.req").remove(); as suggested by you and everyone else.Thank you.Just one id was enough,so I left out that #labelstr.
Angeline Aarthi
then my answer was correct? !!
redsquare
yes.. your answer is correct
Angeline Aarthi
+2  A: 

No need to use 2 id selectors as they 'should' be unique, so just do

$("#labelstr" + div_id + " span.req").remove();
redsquare
A: 

I found out the answer. It is

   $('.req').remove();

Earlier I tried the same,without the quotes, that is why I didn't get the required result.

@machine: Yes,it is jQuery. Thank you.

Angeline Aarthi
But that will remove all elements that have a class or req. Are you sure you want this? Also just using a class in a selector is slow. It is better to use nodeName.class for performance especially on a large dom
redsquare
yes, i was wrong. It removed for all the elements that have that class.Instead using $("#labelstr" + div_id + " span.req").remove();as suggested by you and everyone else. Thank you.
Angeline Aarthi