Find A Pattern In DOM And Replace It's Content Using Jquery
I have a div containing elements something like below
Alerts
Solution 1:
This script should do what you want:
$(document).ready(function(){
// loop over all links inside the table
$("a", "#clicker").each(function(){
var href = $(this).attr('href');
if(href.indexOf("abc.global.com") >= 0){
// replace domain
href = href.replace("abc.global.com", 'cab.global.com');
// append source param
href += "?source=abc";
$(this).attr('href', href);
}
});
});
Solution 2:
Try this:
$( 'div#clicker a' ).each(function(o){
var _anchor = $(this).attr('href');
if(_anchor.indexOf( 'abc.global.com' )!=-1) {
_anchor = _anchor.replace( 'abc.global.com', 'cba.global.com' );
_anchor = modify( _anchor );
$(this).attr('href',_anchor);
}
});
function modify( anchor ) {
if( anchor.indexOf( '?' ) != -1 ) {
anchor = anchor.replace( anchor.indexOf( '?' ), '?source=abc&' );
} else if( anchor.indexOf( '#' ) != -1 ) {
anchor = anchor.replace( anchor.indexOf( '#' ), '?source=abc#' );
} else {
anchor += '?source=abc';
}
return anchor;
}
Hope it helps.
Solution 3:
If it's ok to simply loop through every tag on the whole website you can use
var links = document.getElementsByTagName("a");
to get a list of all of them. Then just loop through them and change href where appropriate
for (var i = 0; i < links.length; i++)
{
if (links[i].href.search("abc.global.com") != -1)
{
links[i].href.replace("abc.global.com", "cab.global.com");
links[i].href += "?source=abc";
}
}
You might want to temporarily save href in a variable to increase performance and maybe add a few safety checks.
Solution 4:
Try this
$('a').each(function(a){
if ($(this).attr('href').toLowerCase().indexOf("abc.global.com") >= 0){
$(this).attr('href',$(this).attr('href').replace('abc.global.com','cab.global.com'));
$(this).attr('href',$(this).attr('href') + '?source=abc')
}
});
Here is a http://jsfiddle.net/MgsrV/
Solution 5:
$('a[href*=abc]').each(function(){
return this.href = this.href.replace('abc', 'cab')+'?source=abc';
});
Post a Comment for "Find A Pattern In DOM And Replace It's Content Using Jquery"