How to add .com
in multiple a
tag on run time using Jquery.
jQuery(document).ready(function($){
var a = $('a').text();
$('a').attr('href',a+'.com');
});
You need to use .each()
like below:-
Working example:-
//convert $ to jQuery if you are using older jQuery library $(document).ready(function($){ $('a').each(function(){ $(this).attr('href',$(this).attr('href')+".com") }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="abc">Abc</a><br> <a href="def">Def</a><br>
Note:- It's required that your links have https://
or http://
in it's href. Because only having abc.com
will not tell that the link have to be https://....
or http://....
to the browser and it will lead to ambiguity sometimes.
Something like that:
jQuery(document).ready(function ($) {
$('a').each(function () {
this.href = this.innerHTML + '.com';
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.