最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Append values into an existing anchor href - Stack Overflow

programmeradmin4浏览0评论

I have captured 3 values from a datalist template using JavaScript, now I need to append these values into an existing anchor which is:

<a id="link" href='nextpage.aspx?id=<%#Eval("PlateId")%>&pp= #Eval("price")%>'>

I found the way to get the anchor href from JavaScript:

<script language='javascript' type="text/javascript" >
    function addLink() { 
        var anchor = document.getElementById("link"); 
        anchor.href = anchor + "&qty=";} 
</script>

But, I can't add the js value after "&qty=", I have tried adding the value like this:

anchor.href = anchor +"&qty=+Value+"

And with this:

anchor.href = anchor +"&qty='Value' "

I can't put it out of the quotation marks, because it won't display in the anchor.

I have captured 3 values from a datalist template using JavaScript, now I need to append these values into an existing anchor which is:

<a id="link" href='nextpage.aspx?id=<%#Eval("PlateId")%>&pp= #Eval("price")%>'>

I found the way to get the anchor href from JavaScript:

<script language='javascript' type="text/javascript" >
    function addLink() { 
        var anchor = document.getElementById("link"); 
        anchor.href = anchor + "&qty=";} 
</script>

But, I can't add the js value after "&qty=", I have tried adding the value like this:

anchor.href = anchor +"&qty=+Value+"

And with this:

anchor.href = anchor +"&qty='Value' "

I can't put it out of the quotation marks, because it won't display in the anchor.

Share Improve this question edited Feb 19, 2013 at 21:56 Flexo - Save the data dump 88.8k22 gold badges201 silver badges281 bronze badges asked Feb 19, 2013 at 21:43 Carlos SilesCarlos Siles 712 silver badges6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4
anchor.href = anchor + "&qty="

should be

anchor.href = anchor.href + "&qty="

...otherwise you are trying to turn the link element into a string!

Meanwhile, you still need to keep Value out of the quotation marks, otherwise the final text generated will always be exactly that text: Value. If Value is a variable and is not returning the value you expect (an empty or undefined value, for example), then you need to look at the code that declares it and change that.

Dynamically change <a href=""></a> href atrribute value (or) You can able to append the existing URL

Tips: 1 - Use the jQuery .attr() Method

 $("selector").attr("href", newURL));

Tips: 2 - Use javascript element selector document.getElementById

example:

<a href="test.php" id="test">TEST URL</a>
var redirect_url = document.getElementById('test');
redirect_url.href = "new url";

(or)

document.getElementById('test').href = "newURL";

(or)

using setAttribute() method *Note:broken in IE

redirect_url.setAttribute("href", "new url");
发布评论

评论列表(0)

  1. 暂无评论