1

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.

1
  • Use $(this).attr('href') in the click handler Commented Dec 30, 2015 at 9:25

2 Answers 2

1

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;
});
Sign up to request clarification or add additional context in comments.

Comments

0

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;
});

3 Comments

actuall i have lots of a href but i could not perform onclick action on every link that's why i can't use "this" suggest some other way
I also use above array method,by using this it will redirect with all three links instead one at a time
@shopeeon Yea, it was confusing. So gave you three different answers. You can choose the one that's best for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.