stupid checkbox question

53 views
Skip to first unread message

Phil Petree

unread,
Apr 26, 2012, 4:48:22 PM4/26/12
to prototype-s...@googlegroups.com
How do you get the checked state of a single checkbox?
 
$('cbID').checked doesn't work...  also tried $F('cbID').checked
 
in the following function, the alert comes back with "undefined"
 
cdValue is the id of the checkbox...
 
function saveChange(cbValue)
{
  var strState;
  var cbState;
 
  // get the value of the checkbox
  // we do it here incase this is called
  // from the onchange attached to the select
  cbState = $(cbValue).checked; 
  alert(cbState);
 
  if( cbState == "checked"  )
    strState = "checked";
  else
    strState = "unchecked";
}

Wojtek Zadora

unread,
Apr 27, 2012, 1:46:08 AM4/27/12
to prototype-s...@googlegroups.com
Well, I tested it and in my case having <input type="checkbox" name="1"
value="1" id="cb1">

console.log($('cb1').checked); -> returns false
console.log($('cb1').readAttribute('checked')); -> returns null

While <input type="checkbox" name="2" value="2" id="cb2" checked="checked">

console.log($('cb2').checked); -> returns true
console.log($('cb2').readAttribute('checked')); -> returns checked

-wz
> --
> You received this message because you are subscribed to the Google
> Groups "Prototype & script.aculo.us" group.
> To post to this group, send email to
> prototype-s...@googlegroups.com.
> To unsubscribe from this group, send email to
> prototype-scripta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/prototype-scriptaculous?hl=en.

Phil Petree

unread,
Apr 27, 2012, 4:02:08 AM4/27/12
to prototype-s...@googlegroups.com

Try it passing cb1 into a function as cbValue and then $(cbValue).checked...

To post to this group, send email to prototype-scriptaculous@googlegroups.com.
To unsubscribe from this group, send email to prototype-scriptaculous+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com.
To unsubscribe from this group, send email to prototype-scriptaculous+unsub...@googlegroups.com.

Wojtek Zadora

unread,
Apr 27, 2012, 4:10:19 AM4/27/12
to prototype-s...@googlegroups.com
$(cbValue).checked returns boolean (true|false) and you compare it to string:


if( cbState == "checked"  )

simply

strState = (cbState == true) ? "checked" : "unchecked";

-wz




To post to this group, send email to prototype-s...@googlegroups.com.
To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.

--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
To post to this group, send email to prototype-s...@googlegroups.com.
To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.

--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
To post to this group, send email to prototype-s...@googlegroups.com.
To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com.

Phil Petree

unread,
Apr 27, 2012, 4:12:22 AM4/27/12
to prototype-s...@googlegroups.com

Dang it... I knew it had to be something stoooopid!!!  LOL

Thanks!  Its 4a here, I'm gonna get some sleep.

Phil Petree

unread,
Apr 27, 2012, 5:04:13 AM4/27/12
to prototype-s...@googlegroups.com
Well, that didn't work...
 
<input type='checkbox' id='1' name='approved' onclick="saveChange(1);" checked='checked'/>
<select id='ajMode_1' name='mode' onchange="saveChange(1);">
  <option value='read_write' selected='selected'>Send and Receive</option>
  <option value='read_only'>Receive Only</option>
</select>
 
function saveChange(cbValue)
{
  var strSerialize;
  var strState;
  var strMode;
  var strSelectName = "ajMode_" +cbValue;

  var cbState;
 
  // get the value of the checkbox
  // we do it here incase this is called
  // from the onchange attached to the select
  cbState = $(cbValue).checked;   
  strState = (cbState == true) ? "checked" : "unchecked";
  // get the value from the selected rw_mode
  strMode = $F(strSelectName);
 
  // serialize the data and send to the server for saving
  strSerialize = "record=" +cbValue +"&state=" +strState +"&mode=" +strMode;
  alert(strSerialize);   // always shows strState as unchecked
  autosave(strSerialize);   // make the ajax call
}


 

Dave Kibble

unread,
Apr 27, 2012, 5:47:51 AM4/27/12
to prototype-s...@googlegroups.com
On 27 April 2012 09:12, Phil Petree <phil....@gmail.com> wrote:
> Its 4a here, I'm gonna get some sleep.

On 27 April 2012 10:04, Phil Petree <phil....@gmail.com> wrote:
> Well, that didn't work...

not enough sleep!

> <input type='checkbox' id='1'

not a legal ID - must start with a letter

> onclick="saveChange(1);"

1 is a number, '1' is a string


> function saveChange(cbValue)

a better name for the parameter would be cbID


Dave

Phil Petree

unread,
Apr 27, 2012, 9:54:22 AM4/27/12
to prototype-s...@googlegroups.com
Dave, you're a gentleman and a scholar...
 
The id was irrelevant because in the original pass I was passing in this.checked but then someone suggested we allow them to change the mode as well so then I couldn't use this.checked with an onchange fired via a <select> and by then it was waaaay to late into the wee hours to see what was wrong.
 
Thanks!

T.J. Crowder

unread,
Apr 28, 2012, 12:35:53 PM4/28/12
to prototype-s...@googlegroups.com, DaveK...@gmail.com
On Friday, 27 April 2012 10:47:51 UTC+1, DaveK wrote:
> <input type='checkbox' id='1'

not a legal ID - must start with a letter 

That's no longer true as of HTML5, which in this case is just documenting what browsers already did:

It *was* true for HTML4, but browsers didn't care in terms of document.getElementById and such.

Note that it's still true for CSS (#1 is an invalid selector), and browsers *do* care about that (e.g., with querySelectorAll and such):

So while HTML5 is very lax, if you're ever going to use CSS selectors to refer to the element, best to stick to the more restrictive CSS rules.

FWIW,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

Phil Petree

unread,
Apr 28, 2012, 2:29:12 PM4/28/12
to prototype-s...@googlegroups.com

That's what I thought too... but adding CB to the I'd name fixed it.

--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
Reply all
Reply to author
Forward
0 new messages