hmav25
hmav25

Reputation: 23

Replacing anchor tag text after it is clicked without removing css

I am changing anchor tag text once it is clicked using jquery (replaceWith) and it is working fine. But it also replaces the css of the anchor tag text.

Can anyone help me how not to replace css or add it again.

My html is as follows:

<div class="col-xs-12 col-md-12" >
    <a class="btn btn-lg btn-success deal" target="_blank" style="width:100%;background:#b71c1c;" rel="nofollow, noindex" 
       href="<?= $coin["link"]; ?>" >
        <b>Get Offer</b>
    </a>
</div>

and code:

$(document).ready(function(){
   $('.deal').click(function(){
       $(this).replaceWith("<?php echo $coin['coupon_code']; ?>");
  });
});

Upvotes: 0

Views: 123

Answers (3)

Mani
Mani

Reputation: 2655

If <?php echo $coin['coupon_code']; ?> contains html tags use

$(this).find('b').html('<?php echo $coin['coupon_code']; ?>');

Or if it is plain text use

$(this).find('b').text('<?php echo $coin['coupon_code']; ?>');

If you don't want bold remove .find('b')

Upvotes: 1

Ben Hillier
Ben Hillier

Reputation: 2104

If you just want to replace the content of the inner <b> tag, this is your best option:

$('.deal').click(function(){
   $(this).find('b').text('<?php echo $coin['coupon_code']; ?>');
});

Upvotes: 0

Kashyap
Kashyap

Reputation: 391

Try this, you just need to change text.

 $(this).html("<?php echo '<b>'.$coin['coupon_code'].'</b>'; ?>");

Upvotes: 0

Related Questions