update href from checkbox click

iceman19330
iceman19330 used Ask the Experts™
on
I have a link that when someone checks a checkbox I need to add something to the link.

<a href="blah?id=1">Blah</a> would be the default and if I checked a box
<input type="checkbox" name="id" value="2" />
 it would be <a href="blah?id=1,2">Blah</a>
and if I check another one
<input type="checkbox" name="id" value="3" />
 it would be <a href="blah?id=1,2,3">Blah</a>

any thoughts?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
$("input[name='id']:checked").click(function() {
       var default = "blah?id=1"
       $(this).each(function() {
           default += "," + $(this).val();
       });
       $("#aId").attr("href", default);
})

Open in new window


With :

<a id="aId" href="blah?id=1">Blah</a>

Open in new window

you can put this to the checboxes onclick action

$("a[href]").attr("href", ""+$("a[href]").attr('href')+','+this.value+"");

Author

Commented:
doesnt seem to be updating,  here is a link to the code.
http://tinyurl.com/3nm46ok

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

leakim971Multitechnician
Top Expert 2014

Commented:
change default with an other variable name, for example defaultURL :
$("input[name='id']:checked").click(function() {
       var defaultURL = "blah?id=1"
       $(this).each(function() {
           defaultURL += "," + $(this).val();
       });
       $("#aId").attr("href", defaultURL);
})

Open in new window

Author

Commented:
nope still will not append the additions from the checkbox.
leakim971Multitechnician
Top Expert 2014

Commented:
sorry remove :checked too else it only apply to already checked items
$("input[name='id']").click(function() {
       var defaultURL = "blah?id=1"
       $(this).each(function() {
           defaultURL += "," + $(this).val();
       });
       $("#aId").attr("href", defaultURL);
})

Open in new window

Author

Commented:
it is only taking from 1 checkbox, if I click both it only keeps the first one.  also if I uncheck it doesnt remove the id.
leakim971Multitechnician
Top Expert 2014

Commented:
Ok, fixed :
$("input[name='id']").click(function() {
       var defaultURL = "blah?id=1"
       $("input[name='id']").each(function() {
           defaultURL += "," + $(this).val();
       });
       $("#aId").attr("href", defaultURL);
})

Open in new window

Multitechnician
Top Expert 2014
Commented:
A new one :
$("input[name='id']").click(function() {
       var defaultURL = "blah?id=1"
       $("input[name='id']:checked").each(function() {
           defaultURL += "," + $(this).val();
       });
       $("#aId").attr("href", defaultURL);
})

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial