Removing text that was appended previously
Asked Answered
E

3

6

I have a highlighting function using JQuery, that changes the css for the clicked <li> element in a menu. The function also prepends a pair of left brackets << to serve as pseudo arrows.

But how do I remove that << when I switch to the next <li> ?

$(".sdv-nrml").click(function(){

//remove old highlighted li 
$(".sdv-nrml").css({'background' : '#ffcc66' , 'color' : '#000000' , 'text-align' : 'right'});

//assign new css and prepend arrow
$(this).css({'background' : '#996600' , 'color' : '#ffff66' , 'text-align' : 'left'});
$(this).prepend("<< ");
});

Thanks

Epigraph answered 27/5, 2011 at 0:33 Comment(0)
M
12

I would include the << in a <span>:

$(this).prepend('<span class="prepended">&laquo; </span');

then to remove:

$(".prepended").remove();

Note: I used « instead of <<. I find it a little more appealing.

Moir answered 27/5, 2011 at 0:38 Comment(1)
note: minor error. class was named "prependED" referred to as .prepend great solution though, exactly what I was looking forPhantasm
P
7

Wrap it in a span with a class and remove that.

$(this).prepend('<span class="pseudo-arrow">&lt;&lt;</span>');
Pharsalus answered 27/5, 2011 at 0:36 Comment(0)
D
0

why you don user a class for the selected state? .selected-item { background: #ffcc66; color: #000; text-align:right; }

So your script can be used like that:

$(".sdv-nrml").click(function(){

    //remove old highlighted li
    $(".sdv-nrml").each(function(){
        $(this).removeClass("selected arrow");
    });

    //assign new css and prepend arrow
    $(this).addClass("selected");
    $(this).prepend("<span class='arrow'>&lt;&lt;</span> ");
});
Droshky answered 27/5, 2011 at 1:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.