Blynn
Blynn

Reputation: 1411

apply param to links inside div from the url value using jquery

So I have a URL like:

www.site.com/nice.html?menu=1

and I'm trying to find all links inside a div called.. ".page" and add "&menu=1" to them based on the menu parameter inside the url string.

I hope this makes sense. I'm going to create a jsfiddle.

Here is my JS so far.

$(".page a").each( function(){
//get page param with getmenu    
var getmenu = "";
var attr = $(this).attr("href"); 
attr+"&menu="+getmenu; 
}); 

http://jsfiddle.net/kWAxe/

Upvotes: 1

Views: 107

Answers (2)

Pallab
Pallab

Reputation: 285

Check this. You have to reset the variable back to the href attribute. Alos you need to correct the name of your variable.

var gemenu = 0;

$(".page a").each( function(){
    //get page param with getmenu    
    gemenu += 1;
    var attr = $(this).attr("href"); 
    attr += "&menu="+gemenu;
    $(this).attr("href", attr);
}); 

http://jsfiddle.net/kWAxe/2/

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

How about:

$(".page a").each( function(){
    //get page param with getmenu    
    var getmenu = location.href.split("?")[1];
    var attr = $(this).attr("href"); 
    $(this).attr("href", attr + "&" + getmenu);
}); 

Upvotes: 1

Related Questions