3

I have this:

<span id="social">
    Facebook:
    <input id="id_connected_to_facebook" type="checkbox" name="connected_to_facebook">
    <br>
    Twitter:
    <input id="id_connected_to_twitter" type="checkbox" name="connected_to_twitter">
    <br>
</span>

and with jQuery I would like to call a different URL for all the 4 possible actions (1: check facebook, 2: uncheck facebook, 3: check twitter, 4: uncheck twitter).

How would I do that?

1
  • Don't use onchange method. It's going crazy in MSIE Commented May 4, 2012 at 17:25

3 Answers 3

9

I am not sure what you are going for. If you want to redirect to a page on clicking on the check box you can do this

Specify what url you want to call in your checkbox element using data attribute

<input type="checkbox" data-target="http://www.facebook.com" />

Script is

$(function(){
  $("input[type='checkbox']").change(function(){
  var item=$(this);    
  if(item.is(":checked"))
  {
    window.location.href= item.data("target")
  }    
 });
});

Sample JsFiddle : http://jsfiddle.net/45aZ8/2/

EDIT : If you want to open it in a new window, use window.open method instead of updating the current url

Replace

window.location.href= item.data("target")

with

window.open(item.data("target")

http://www.w3schools.com/jsref/met_win_open.asp

EDIT 3 : Based on comment : If you want to load one url on Checked state and load another url on uncheck, you can do like this

Give 2 data attributes for each checkbox. One for checked state and one for unchecked

Facebook<input type="checkbox" data-target="http://www.facebook.com" data-target-off="http://www.google.com"  /> <br/>
Twitter<input type="checkbox" data-target="http://www.twitter.com" data-target-off="http://www.asp.net"  /> <br/>

And the script is

$(function(){
  $("input[type='checkbox']").change(function(){
  var item=$(this);    
  if(item.is(":checked"))
  {
       window.open(item.data("target"))   
  }
  else
  {
      window.open(item.data("target-off"))   
  }        
 });
})

Working sample : http://jsfiddle.net/45aZ8/5/

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

6 Comments

@core1024: yea. Thanks thecodeparadox for jumping in and fixing.
Sorry I was not very clear, I want the current page to stay open. I want the user to associate the account to a social one and for that I use some URLs. It's just a matter of loading the URL but since the checkboxes are on a profile page it would not be convenient to leave the page. I'm not sure how to handle that with jQuery.
It looks good but I'm not fluent in jQuery, where should I put my 4 URLs (FB connect and disconnet and Twitter connect and disconnect)? And I'm not sure but I believe this can be linked to the id of the span right? Because I have more checkboxes on this page.
@Bastian: Check my example jsfiddle.net/45aZ8/2. Use the HTML5 data attribute to store the url
ok I see but I need to load a different URL when the checkbox is unchecked. Basically when it is checked it should load something like facebook/connect and when it is unchecked it should go to facebook/disconnect and the same for the other one.
|
0
$('input[type="checkbox"]').change(function() {
  if($(this).is(':checked')) {
     if($(this).attr('id') == 'connected_to_facebook') {
        // possibility: 1
     } else {
       // possibility: 3
     }
  } else {
    if($(this).attr('id') == 'connected_to_facebook') {
       // possibility: 2
     } else {
       // possibility: 4
     }
  }
});

Comments

0
$('#id_connected_to_facebook, #id_connected_to_twitter').click(function(){
   $.post('http://target.url', {this.id:this.checked});
});

now you'll get in $_POST either id_connected_to_facebook or id_connected_to_twitter and as a value it would be 1 or 0

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.