You are viewing limited content. For full access, please sign in.

Question

Question

How do I correctly populate a string value into a field when a checkbox is ticked?

asked on May 28, 2015 Show version history

I'm not sure what I'm doing incorrectly but I'm attempting to populate a string value into a field based upon a checkbox being ticked and clear the string when it's unticked.

  • Checkboxes = #q12
  • The first checkbox in a group of 11 has the ID of #Field12-0
  • The individual input field I'd like to populate with a string is #q1
  • These checkboxes are also being used to trigger show/hide lookup rules (in case that impacts the discussion).

Any help pinpointing what I'm doing incorrectly would be fantastic, I've tried this a few ways and am obviously just doing something really wrong. 

$(document).ready(function () {
  $('#Field12-0 input').change(function() {
    if ($('#Field12-0 input:checked').val() == 'yes'){
      $('#q1 input').val("yes");
    }
    else {
      $('#q1 input').val("");
    }    
  }  
});

Thanks for any advice!

0 0

Answer

SELECTED ANSWER
replied on May 31, 2015 Show version history

Hi Rene,

Thanks for your reply, unfortunately that particular solution didn't work for me, but I appreciate the post as it looks like something that will come in useful for the future.

Working on the issue today I've managed to get this working correctly using the following.

$(document).ready(function () {
	$("#q12").change(function () { 
		if ($("input[id^=Field12-0]").is(':checked')){
		      	$('#q1 input').val("yes");
		}
		else{
			$('#q1 input').val("");        
		}
	})
});  

The specific thing to watch out for is the syntax around each checkbox field ID. Now I know the syntax includes the square brackets and 'id^=' I've been able to get back to just verifying if something is checked at all - rather than the value it has.

$("input[id^=Field12-0]").is(':checked')

EDIT: I've marked this as the solution (because it was) but if we're not supposed to tick our own responses please just let me know and untick it. 

1 0

Replies

replied on May 29, 2015 Show version history
$(document).ready(function () {
$('#q12 input').change(function () {               
   	if ($('#q12 input:checked').val()=== "yes"){
            $('#q1 input').val("yes");
        }
        else {            
            $('#q1 input').val("");
        }
  })
 });  

Try this one, the yes in the line if ($('#q12 input:checked').val()=== "yes"){

Should be the name of the checkbox.

 

here an example of something works for me

and the code to make it works

// Hide or show credit card information section 
  $('.paymentbox input').change(function () {               
   		if ($('.paymentbox input:checked').val()=== "Credit Card"){
            $('.credit').show();
        }
        else {
            $('.credit').hide();
        }
  })            
// end Hide and show credit card

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.