lakki
lakki

Reputation: 125

Setting href to Hyperlink text

I am getting below text from database with href as null.

You also have to click on this link .

This text I am assigning to the label when page loads. My requirement is when I click the "link" I need to redirect to certain page. Where should I assign the href value and how?

Upvotes: 3

Views: 5246

Answers (4)

palaѕн
palaѕн

Reputation: 73886

document.getElementById('myAnchor').href="http://www.google.com";

Simple enough :)

In case, you also want to Change URL and text of the hyperlink...

<html>

<head>
<script type="text/javascript">
function myHref(){
    document.getElementById('myAnchor').innerHTML="Visit Google"
    document.getElementById('myAnchor').href="http://www.google.com"
}
</script>
</head>

<body>
    <a id="myAnchor" href="http://www.java2s.com">Visit Java2s</a>
    <form>
        <input type="button" onclick="myHref()" value="Change URL and text">
    </form>
</body>

</html>

Source: java2s

Upvotes: 0

Wolf
Wolf

Reputation: 2150

In the aspx page you can write the following line to create a placeholder

<div><%= linkValue %></div>

And in the code behind create a global variable

    string linkValue = "You also have to click on is <a href='' target='_blank'> link </a>"; 
    /* Set the page URL where you want to redirect the page. in your case get the value from database. */";

I think this is what you want to do. It was not completely clear from the question.

Updates: In case you have multiple links in the page you need to create the link along with label. See the code below

HyperLink link1 = new HyperLink();
link1.Text = "LINK HERE";
link1.NavigateUrl = "http://somedomain.com";

And add to the container like this

container.Controls.Add(link1);

Upvotes: 1

Kyle Nunery
Kyle Nunery

Reputation: 2049

If you are using jQuery you can set the href using the following code:

$("a").attr("href", "http://stackoverflow.com");

Upvotes: 1

Talha Akbar
Talha Akbar

Reputation: 10030

<script>
var s = document.getElementByTagName("a");
s.href = ""; // here give the href value
</script>

Upvotes: 1

Related Questions