shopeeon
shopeeon

Reputation: 151

How can i take multiple url value in single variable name

In below I want to pass a variable value from one page to other using url redirection. But in my variable I have multiple url, so that when I click on particular url only that url value will store and redirected.

<script src="jquery-1.11.3.js"></script>
<script>
$(document).ready(function(){
	
   $('a').click(function(e){
	e.preventDefault();
	var clk=$('#fkt').attr('href');
	var clk=$('#snd').attr('href');
	var clk=$('#amz').attr('href');
	//alert(clk);
	window.location='http://www.shopeeon.com?ver='+clk;

	});
});
</script>

<a id='fkt' href="http://www.google.com">Google</a><br/>
<a id='snd' href="http://www.yahoo.com">Yahoo</a><br/>
<a id='amz' href="http://www.rediff.com">Rediff</a><br/>

In my case it will only take last URL link and stored in clk variable.

Upvotes: 1

Views: 291

Answers (2)

A1rPun
A1rPun

Reputation: 16847

To get the href of the current clicked <a> tag use this:

$('a').click(function(e){
    e.preventDefault();
    var clk = $(this).attr('href');
    window.location='http://www.shopeeon.com?ver='+clk;
});

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

The reason why it doesn't work is, you are redeclaring the same string again and again, and it wipes out old content. You may use this:

var clk = $('#fkt').attr('href');
clk += $('#snd').attr('href');
clk += $('#amz').attr('href');

This concatenates the string.

Can you use an array?

var clk = [];
clk.push($('#fkt').attr('href'));
clk.push($('#snd').attr('href'));
clk.push($('#amz').attr('href'));

console.log(clk.join(","));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a id='fkt' href="http://www.google.com">Google</a><br/>
<a id='snd' href="http://www.yahoo.com">Yahoo</a><br/>
<a id='amz' href="http://www.rediff.com">Rediff</a><br/>

You should use the contextual this. But ultimately, what you are looking for is:

$('a').click(function (e) {
  e.preventDefault();
  var clk = $(this).attr('href');
  location.href = 'http://www.shopeeon.com?ver='+clk;
});

Upvotes: 0

Related Questions