Avatar of iceman19330
iceman19330

asked on 

update href from checkbox click

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?
jQueryJavaScript

Avatar of undefined
Last Comment
leakim971
Avatar of leakim971
leakim971
Flag of Guadeloupe image

$("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

Avatar of el_aristo
el_aristo

you can put this to the checboxes onclick action

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

ASKER

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

Avatar of leakim971
leakim971
Flag of Guadeloupe image

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

Avatar of iceman19330
iceman19330

ASKER

nope still will not append the additions from the checkbox.
Avatar of leakim971
leakim971
Flag of Guadeloupe image

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

Avatar of iceman19330
iceman19330

ASKER

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.
Avatar of leakim971
leakim971
Flag of Guadeloupe image

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

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo