Anjana Sharma
Anjana Sharma

Reputation: 4745

onclick checkbox event

All I am trying to do is onclick of checkbox, append its value to the URL and redirect..How do I do this??

$(document).ready(function() {   
    var url = 'http://mysite.com/results.aspx';   
        $('.LocType').click (function ()
            {
                var thisCheck = $(this);
            if (thischeck.is (':checked'))
             {
            // Do stuff
             window.location.href = "http://www.yahoo.com";  

             }
        });
}); 

<div class="MyOptions"> 
    Hospitals<input class="LocType" type="checkbox" value="Hospital"/> &#160;  
    Offices<input class="LocType" type="checkbox" value="Office"/> &#160;  
    Facilities<input class="LocType" type="checkbox" value="Facility"/> 
</div> 

Upvotes: 0

Views: 1287

Answers (2)

Juan Antonio Orozco
Juan Antonio Orozco

Reputation: 750

if you set up your function to recive a parameter javascript would give you an event that have the ellement that fire the event, you cuold do archive this whit the next code:

$('.LocType').click (function (e) {
   if (this.checked) {
       // Do stuff
       window.location.href = "http://www.yahoo.com" + e.target.value;
      //e.target is the element that fire up the event
   }
});

Upvotes: 0

nnnnnn
nnnnnn

Reputation: 150030

That's really not a good UI design - users do not expect navigation to occur via checkboxes - but here's the code you want:

$('.LocType').click (function () {
   if (this.checked) {
       // Do stuff
       window.location.href = "http://www.yahoo.com?paramNameHere=" + this.value;
   }
});

You don't need $(this) inside the function because this is enough to just check the checked state and get the value.

You didn't say what the resulting URL should look like, so I've just appended the value with a generic paramNameHere parameter.

Upvotes: 1

Related Questions