![]() |
using a HREF to check and uncheck a box
Hi I want to use a single href link to toggle the checkbox to check and uncheck. I am using Jquery and playing and adapting some code have got it so that it will check, however I cant seem to get it to uncheck unless I use another seperate href, and that is not an option!
Check out my fiddle http://jsfiddle.net/c4Mju/8/ |
You need to test if the checkbox is checked or not before checking it - reverse the value it currently has instead of setting it true
|
generally not a good idea to have IDs starting with numbers, but if you were to go that way you could reduce your entire jQuery to:
Code:
$(document).ready( function() { |
I can get it to do it based on the state, however the else statement isnt working.
jsfiddle - without the else jsfiddle with the else = fail! Any help is much appreciated! Jquery/Javascript is not my strong point :confused: |
Quote:
Code:
<input id="124" type="checkbox" />One</br> |
Quote:
But anyway. If you cannot tell in advance what the IDs are going to be, what is the point in trying to write a function using hardcoded IDs? Wouldn't it be better to say that (for example) the first link refers to the first checkbox, the second to the second, etc? |
itxtme, a more general question: what is the point of you using the href attribute if it’s not referencing anything?
|
What I am doing is pulling a dynamic list of products.
Each product has a check box that by default will be checked, instead of the user having to click on the check box I want them to be able to click on the href encasing the name of the product. Your code looks to be what I am after, however I want to just match one link to one product and toggle. This jsfiddle gives a better representation of the products The actual jquery script code will be dynamically generated in php at page load. What I am trying to get is a piece of jquery code that will use the href unique class/id to toggle the checkbox with a matching class/id Cheers EDIT: the actual UI http://imageupper.com/s02/1/1/I13557019671243749_1.jpg |
there are various ways of doing this, particularly being that the structure of your page will be so uniform you could also do it using DOM methods like prev() and next() but this is what I was talking about:
Code:
$(document).ready( function() { |
Thanks, exactly what I am after!
|
Quote:
That said, I don’t get the point of the whole thread here if all you wanna do it click on a text string and want to check/uncheck a checkbox because that’s exactly what the <label> element is doing. Why make things so complicated?Code:
<input type="checkbox" id="example">Code:
<label><input type="checkbox"> If this text inside the label element is clicked the checkbox will be checked/unchecked</label> |
The reason is it is making an entire<li> item clickable, it makes it easier from a end user point of view to select or unselect the item. UI for the win.
|
There is nothing that an anchor can do better than a label element in this case. Semantics and accessibility for the win. Oh, and CSS, too, of course, because you can style your labels to fill the entire list items. As I said, an anchor is not any better than a label here (in fact, it’s the wrong choice, even from a UI perspective).
|
I entirely understand what you are saying, however I am using a UI framework. So to remove it would cause the secondary link to fail. Small price to pay I think
|
| All times are GMT +1. The time now is 12:48 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.