views:

29

answers:

3

Hi,

I’m trying to insert the url from an onlick event in the the radio buttonsto the href link but it not working. Here’s I have so far.

<input type="radio" name="orderID" value="1"  onclick="javascript:document.getElementById('editBTN').href='forms/editForm.cfm?orderID ='&this.value">

<input type="radio" name=" orderID " value="2" onclick="javascript:document.getElementById('editBTN').href='forms/editForm.cfm?orderID ='&this.value">

Etc…

The link below href should change depending on which radio button is clicked.

<a href="" id="editBTN"> Edit Order</a>

The above script returns the current url with “localhost/myApp/0” at the end. If I remove this ('forms/editForm.cfm?orderID='&) from the radio button it correctly return the orderId.

I would like this result localhost/myApp/forms/editForm.cfm?orderID=1. Any suggestion would be greatly appreciated.

A: 

I think the & before this.value should be a +

patrickmcgraw
yep that was it, thanks for your reply!
+1  A: 

JavaScript uses + to concatenate strings, not &:

onclick="document.getElementById('editBTN').href=
         'forms/editForm.cfm?orderID ='+this.value"

(line break added for readability) should work.

You can lose the javascript: prefix, by the way.

Pekka
Thanks!!! I totally forgot about the "+".
+2  A: 

You listed jquery as one of your question tags. I'd simply place the following script onto my page and attach the click event on the radio's to push their value into the appropriate button attribute.

$('input[name=orderID]').click(function(e) {

$('#editBTN').attr('href', 'forms/editForm.cfm?orderID ='+$(this).val());

});
Brian Scott
+1 just remember that this needs to be put in jQuery's "document ready" event.
Pekka
Your suggestion works too, thank you!
@user281867, no problem, glad it helped. Please mark the answer correct if you are using this solution. :-)
Brian Scott