Currently I'm just using jQuery to have a checkbox and a text, with the text to change after the checkbox is ticked, but I'm trying to work it so that the box OR text can be clicked and the result is changed text and checked box?
Can anybody assist me further?
Currently I'm just using jQuery to have a checkbox and a text, with the text to change after the checkbox is ticked, but I'm trying to work it so that the box OR text can be clicked and the result is changed text and checked box?
Can anybody assist me further?
Share Improve this question edited Aug 26, 2013 at 13:43 j08691 208k32 gold badges269 silver badges280 bronze badges asked Aug 26, 2013 at 13:42 50dollanote50dollanote 1894 gold badges4 silver badges12 bronze badges 2- 1 Please post the code you're already tried. – j08691 Commented Aug 26, 2013 at 13:44
- what hv you tried so far? – Nitesh Commented Aug 26, 2013 at 13:45
6 Answers
Reset to default 5just put a label text around the checkbox and text, so it bees something like:
<label><input type="checkbox" />Add to cart</label>
This makes the text part of the checkbox
see DEMO
in html
<input type="checkbox" id="test"> <label for="test">text</label>
in JS
$(document).ready(function () {
$("#test").click(function () {
if ($(this).is(":checked")) {
$(this).next("label").text("Added");
} else {
$(this).next("label").text("Add to Cart"); // your default color set
}
});
});
reference checked
jQuery.next()
using label and setting the for attribute, you can click at the text, and the box will be checked/unchecked...
<label for="check1">Label for checkbox</label>
<input type="checkbox" id="check1" value="value1">Option</input>
DEMO
Try this
$("#chk").click(function () {
if ($(this).is(":checked")) {
$(this).next().fadeOut("slow", function () {
$("#chk").next().text("Added");
}).fadeIn("slow");
} else {
$(this).next().fadeOut("slow", function () {
$("#chk").next().text("Add to cart");
}).fadeIn("slow");
}
});
html
<input type="checkbox" id="chk">
<label for="chk" style="text-decoration:underline">Add to cart</label>
Hope this helps,Thank you
HTML
For the HTML part, there are two ways to specify a label for an element:
Setting the label's
for
attribute to the element's id<input type='checkbox' id='chkCart' /> <label for='chkCart'>Add to Cart</label>
Placing the element inside a label
<label > <input type='checkbox' /> Add to Cart </label>
I'll use the first one
JavaScript
Then you can use jQuery to handle the rest:
- First, make sure the DOM is loaded by wrapping your javascript in a
.ready
function (since we'll be looking to modify these elements). - Then attach an event handler to the checkbox click event by using the
.click
function. - Since we've set the
for
attribute for this particular label, we can use theattribute
equals selector ([name="value"]
) by looking for a label with the samefor
value as our calling element'sid
. - You can check if the object that raised the event is checked on or off by using the
:checked
selector. - You can use the Javascript's
ternary
operator to conditionally set each value - Then rename the label using the
.text
function
It should all look like this:
$(function () {
$("#chkCart").click(function () {
var lbl = $(`label[for='${this.id}']`);
var msg = this.checked ? "Added" : "Add to Cart"
lbl.text(msg);
});
});
Demo in Stack Snippets & jsFiddle
$(function () {
$("#chkCart").click(function () {
var lbl = $(`label[for='${this.id}']`);
var msg = this.checked ? "Added" : "Add to Cart"
lbl.text(msg);
});
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<input type='checkbox' id='chkCart'/>
<label for='chkCart'>Add to Cart</label>
You can associate onChange event to your checkbox , if it's checked , you show your text,else you show the link . DEMO HERE
HTML code:
<input type='checkbox' id='check'/>
<label><a href='#'>add to cart</a></label>
JavaScript/jQuery Code :
$('#check').change(function(){
if ($(this).is(":checked")) $(this).next().text('added');
else $(this).next().html("<a href='#'>add to cart</a>");
});