1

I have some dynamically created links in my pages and what I need to do is when I click on the link, I should pass the name of the link to another page. So, please show me a way to accomplish this. Thanx in advance :)

2
  • 3
    Can you explain more detalied what you want to do? What means "should pass the name of the link to another page"? Commented Mar 19, 2011 at 16:22
  • When I said name of the link i.e <a href = "some website">Name_of_the_link</a> . This is name of the link ! Commented Mar 19, 2011 at 16:25

4 Answers 4

1

If you are using jquery, you can use :

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<a href="#foo">foo</a> <a href="#bar">bar</a>
<script>
  $('a').click(function () { alert($(this).text()); /* attr('href') */ });
</script>
</html>

Of course the html an so are not good, but the click thing is what you need. Then you could use ajax, or a parameter in the target URL to give the link to the other page.

But why don't you generate that when creating links? I mean add a href="mylink?from=mylink".

Edit: corrected with your comment. What you need is text() rather than attr('href').

Sign up to request clarification or add additional context in comments.

Comments

1
document.getElementById('anchor').innerHTML;

then you can pass this value as a parameter, to another page.

Comments

1
documetn.getElementById("mylink").setAttribute("href", "newlink");
documetn.getElementById("mylink").InnerHTML = "new link name";

Comments

1
var link = document.getElementById('link');
link.setAttribute("href", link.getAttribute("href") + "?linkName=" + encodeURI(link.innerHTML));

This will make your link be something like this:

<a id="link" href="http://somewebsite.com?linkName=Name_of_the_link">Name_of_the_link</a>

Then on the "other" page you can access the link name through the GET variable linkName.

Comments

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.