Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text next to checkbox IF checked. HTML + JS or jQuery [closed]

I just want to use a simple checkbox, for example, a checkbox with 'Please check this' next to it, then when it's clicked the text changes to 'Checked'

I've been looking all over for a simple solution, but struggling and can't figure it out - despite the simplicity.

Can somebody help me?

like image 681
50dollanote Avatar asked Aug 24 '13 20:08

50dollanote


People also ask

How do you check checkbox is checked or not jQuery?

prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.

How do you check a checkbox is checked or not in Javascript?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.


1 Answers

Using jQuery:

$('#boxid').click(function() {
  if ($(this).is(':checked')) {
    $(this).siblings('label').html('checked');
  } else {
    $(this).siblings('label').html(' not checked');
  }
});

and in your HTML:

<input id="boxid" type="checkbox"><label for="boxid">not checked</label>

EDIT:

Working fiddle: http://jsfiddle.net/zpzxM/

like image 124
Positivity Avatar answered Oct 17 '22 13:10

Positivity