5

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.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Carlos Siles
  • 71
  • 2
  • 6

2 Answers2

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.

Barney
  • 16,181
  • 5
  • 62
  • 76
  • Hi Barney, when I write the alert(anchor + "&qty=" + qty + "&preference=" + rb + "&extra=" + text) the result is what I am looking for: http://localhost:5437/Pages/nextpage.aspx?id=00001&pp=8.00&qty=2&preference=Chicken&extra=YES so if I do this: anchor.href = anchor or anchor.href +("&qty=" + qty + "&preference=" + rb + "&extra=" + text); nothing happens, so I believe if the alert is giving me the result I want, should be a way to do this. – Carlos Siles Feb 19 '13 at 22:31
  • The code has changed from what you've submitted in your question — you should update the question with this crucial information. But when you say 'nothing' happens, what do you mean? Is there an error? Is the link href unchanged? If you place the alert immediately before `anchor.href = ` assignment, the expected results should appear. – Barney Feb 20 '13 at 13:27
  • problem solved, I used a different aproach. thanks. How would you get the value of aspx radiobutton and aspx check box with javascrip? I am using this unction getvalue(rb), but this is only working with html controls. Thank you in advance man. – Carlos Siles Feb 20 '13 at 21:23
  • This kind of question has been asked [plenty of times](http://stackoverflow.com/search?q=get+radio+value+with+javascript) on StackOverflow. Here's [just one](http://stackoverflow.com/questions/9618504/get-radio-button-value-with-javascript) of many that will probably help. Try Googling a bit, most common Javascript problems have been solved countless times. – Barney Feb 20 '13 at 22:26
  • I made a lot of research about this problem but all the solutions I found are for html input radiobuttons but they are not helpfull for ASPX radibuttons – Carlos Siles Feb 21 '13 at 08:51
  • Carlos, ASPX is a language that the server uses to produce HTML, which is sent to the browser, where Javascript can manipulate it. ASPX radio buttons become HTML radio buttons. – Barney Feb 21 '13 at 09:57
  • Barney, thanks for explaining that. I must be doing something wrong. The weird this is that the function I used function (val) in a html radio returns the selected radio value, but I tested in the aspx radio and nothing happened. So I decided to used html radios in all my pages, and I found a way to display the radios and labels that only have values, here is my dilemma again, I am using a datalist template to display all the items, so when I apply this function in the – Carlos Siles Feb 22 '13 at 04:25
  • the function does its work but only in the first item of the datalist, it looks like it doesn't recognizes the other items. function validation() { var r1 = document.getElementById("RadioButton1");if (r1.value != "") { r1.style.display = 'inline';} is any way to make go through all the datalist items? appreciate that you are taking your time to response my question, this is my first e-commerce site and I am so close to finish it but i am struggling in these small details. have a great day! – Carlos Siles Feb 22 '13 at 04:25
  • You're missing the point: leave the radio buttons as ASPX: all ASPX gets turned into HTML _automatically_ before being sent out to the browser. Load your page, right click, view source: it's HTML. Without modifying the ASPX, all the advice about radio buttons holds true. If you're still struggling, google some more. If you're still struggling after that, ask a new question. – Barney Feb 22 '13 at 09:20
0

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");
venkatskpi
  • 800
  • 6
  • 8