Pawan
Pawan

Reputation: 32321

How to add a class dynamically to a href?

I have been provided with this css

<a href="#" class="tpActive">Butter Extra - Rs. 25/-</a>

I have been trying this way

 $(document).on("click", ".secclass a", function (e) {

 //  if(whetherchecked)
   // {
var name = $(this).attr("name");

var topping='<div class="Topping-details"><section><a href="#">'+name+'</a></section></div>';
//$( "a" ).addClass( "tpActive" );
//$(this).addClass(background-color: Green!important);
$( this ).addClass( "tpActive" );
        $("#myordersdiv").append(topping);
        clcik++;
        $("#myordersdiv").find("i.myorderhead22").text(clcik);
        $("#myordersdiv").show();
        //toppings.push(name);
   // }
});

But the selected element is not being reflected with that class ?? could anybody please help me ??

This is the complete css

<div class="Topping-details">
<img src="images/arrow-topping.png"/>
<section><i id="topping-close"></i>
<a href="#">Ketchup Extra - Rs. 25/-</a>
<a href="#" class="tpActive">Butter Extra - Rs. 25/-</a>
<a href="#">Cheeze Extra - Rs. 25/-</a>
</section>
</div>

Upvotes: 0

Views: 140

Answers (1)

Amin Jafari
Amin Jafari

Reputation: 7207

$(document).on("click", ".secclass a", function (e) {
var name = $(this).attr("name");
var topping='<div class="Topping-details"><section><a href="#">'+name+'</a></section></div>';
$( "a" ).each(function(){
    $(this).addClass( "tpActive" );
    });
});

let me know how it goes.

Upvotes: 1

Related Questions