0

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> 
1
  • Are you trying to make navigation? If so, you might want to rethink it - that's terribly inaccessible. Commented Feb 24, 2012 at 23:40

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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
   }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.