tibin mathew
tibin mathew

Reputation: 2084

variable insertion in href attribute of <a> tag

I have a variable with this value

var= "http://www.coolsite.com"

this variable will change, its a dynamic value

i want insert this variable in my href attribute of tag.

after insertion it should look like this

<a href="http://www.coolsite.com">http://www.coolsite.com</a>

i want to do this in asp.net c#

does any one have an idea, how can i do this?

Thanks

Upvotes: 0

Views: 2824

Answers (4)

senfo
senfo

Reputation: 29076

In the markup, this can be achieved with the following:

<asp:HyperLink ID="HyperLink1" NavigateUrl="http://www.coolsite.com" runat="server">http://www.coolsite.com</asp:HyperLink>

Notice the NavigateUrl attribute. This is the URL that will be placed inside of the href. The inner text is the text that is rendered to the client. Knowing this, you can achieve the same results with this code in your code behind:

string yourUrl = "http://www.coolsite.com";

this.HyperLink1.NavigateUrl = yourUrl;
this.HyperLink1.Text = yourUrl;

Upvotes: 2

Jronny
Jronny

Reputation: 2294

<a id="theLink">http://www.coolsite.com</a>

so to code in jquery:

<script src="jquery.js"></script>
<script>
     $(document).ready(function() {
         $("#theLink").attr("href", "http://www.coolsite.com");
     });
</script>

Upvotes: 0

Abdel Raoof Olakara
Abdel Raoof Olakara

Reputation: 19353

If the var="http://www.coolsite.com" is a javascript variable. you could use javascript to modify the href attribute of a anchor tag. If you use jQuery you can use the attr method.

$('Atag').attr({href:"http://www.coolsite.com"});

If you use plain js code, you can use:

co = document.getelementById('AtagID');
co.setAttribute('href',URL);

Upvotes: 0

monksy
monksy

Reputation: 14234

If you variable is on the server side use an asp:Hyperlink instead and set it when the value gets changed.

Upvotes: 2

Related Questions