Forum Moderators: open

Message Too Old, No Replies

one simple question

concerning checkboxes and variables

         

mcibor

9:25 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have lots of generated checkboxes named 1, 5, 187, etc.
I would like to make a function that uses the clicked checkbox

function check(a)
{
document.form.a.checked = 1;
}

however this way it doesn't work.
How to pass into the function which checkbox is it? (What to write in head and what in body)

Thanks for any answer!

Michal Cibor

PS Summarising I would like to make a function that does e.g.
<input type="checkbox" name="24" id="24" onclick="javascript: if(document.form.24.checked == 1) blabla;">
however I need access to number 24 as well.

Best greetings!

PS2. This is to remeber what checkbox is clicked while I do paginating. I do it by putting the id numbers into the cookie.
If there's a simpler way I would appreciate any answer.
Bye

Rambo Tribble

11:30 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is not legal for a name or an id to start with a number.

coopster

1:49 am on Mar 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I agree on the id but for the <input> name attribute it is quite legal. That is a very common misconception and you'll see erroneous posting by many folks all over the web on this topic. In this particular case, a closer examination of the standard is required:

[webmasterworld.com...]

There is a big difference between CDATA and NAME. An <input> name attribute is of type CDATA, quite different in contrast to say, the id [w3.org] attribute.

mcibor, why not just use the JavaScript "this" keyword in your onclick event? This way you can pass a reference to the object to your function.

mcibor

8:35 am on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK. I can pass "this", and I think I shall do it, but tell me how to extract number 24 from "this"? I suppose "this" will be sth like:
document.form.checkbox so how do I extract checkbox?

BTW At this moment I'm not using plain number, but c24 (c in front).

So How do I extract the desired number from "this" I would like to store just the number in cookie.

And what time shall I place in the cookie to make it a session cookie?

Thanks for any answer.

Michal Cibor

Rambo Tribble

1:54 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, quite a few people seem to be confused by the conventions surrounding names and ids. It would appear that Danny Goodman and David Flanagan are among their number. To quote Flanagan: "The rules for legal identifier names are the same for JavaScript as they are in Java and many other languages. The first character must be a letter, an underscore(_), or a dollar sign ($)."

Bernard Marx

2:57 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That goes for the comp.lang.javascript FAQ too: [jibbering.com...]

I wrote to Richard Cornford a while back, quoting the thread that Coopster mentioned. No response so far.

On a related, I have a copy of Goodman's DHTML - The Definitive Reference, which contains this propagation of a fundamental misunderstanding ofthe JS object model:

"Use the prototype property to assign new properties and methods to future instances of arrays created in the current document" (my italics)

The 2002 publication date suggests that the author should have known better by this stage. It's not that unusual to find coding errors, or simple wrongness, even in official documentation.

I have since developed the attitude that any information from any source is potentially incorrect. I may - of course, only very occasionally - even go as far as to question Mr. Tribble.

coopster

2:59 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Guess that depends on what his (Flanagan) interpretation of a "legal identifier name" is. If it would be the id [w3.org] Element identifier, then he would be correct.

Here nor there, you are right as I see it in plenty of misguided PHP developers blogs, etc. too. BTW, I don't mean to sound *corrective* by any means, just trying to share understanding. Thanks for taking it that way.

mcibor, does this get you started in the right direction?

function myfunction(box) 
{
alert(box.value);
}
<input type="checkbox" name="24" onclick="myfunction(this)" />

<edit>added some clarification (Bernard Marx) slipped a post in there on me ;-) </edit>

Rambo Tribble

3:43 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As regards Mr. Flanagan's application of the word "identifier", his discourse indicates he wishes the term to apply to all names, ids, and variables within the JavaScript context. In essence, this would suggest that starting a name with a number is fine, unless you plan to use that name in a script. Then it would be in the hands of the interpreter implementation to decide your fate.

Section 7.6 of ECMA-262 would tend to support this interpretation (all puns intended).

rocknbil

4:36 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BTW At this moment I'm not using plain number, but c24 (c in front).... How do I extract the desired number from "this" I would like to store just the number in cookie.

Well here is one simple solution to your simple question. What's being discussed here has been confusing to me also and a scheme I came upon a long time ago is to split on an agreed character to separate tags and numeric ID's.:-) I've changed your numbering scheme as shown to use the underscore as that character.

By passing the object itself to the function using "this," any of the object's properties can be accessed (if they exist.) I changed "a" to "obj."

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sample</title>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" >

<script language="javascript">
function check(obj) {
var name = obj.name; // or use getElementByID
var sp = name.split('_');
// obj.checked = 1; // Don't know why you had this. It disables unchecking.
if (obj.checked == 1) { alert(' ' + sp[1] + ' is checked.'); }
else { alert(' ' + sp[1] + ' has been unchecked.'); }
}
</script>

</head>
<body>
<form>
<input type="checkbox" name="c_24" id="c_24" onclick="check(this);"> Number 24
<input type="checkbox" name="c_25" id="c_25" onclick="check(this);"> Number 25 <br>
<input type="checkbox" name="c_26" id="c_26" onclick="check(this);"> Number 26 <br>
</form>
</body>
</html>

EDIT: If you pass the object there's no need to reference document.form - unless you need to submit a form from that function, in which case you pass the form object via "this.form:"

<input type="text" onChange="someFunction(this.form);">

function someFunction(form) {
alert(form); // will give '[object]' referencing form object
form.submit();
}

mcibor

6:40 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks coopster, but that didn't help.
Rocknbil thank for the working function (checked = 1 was just to place sth, not the working code)

However I found a much simpler way to pass object and to get number from it. As I said I'm generating this html, so I'll just use the two input function:

function check(obj, no)
{
}
-----
<input type="checkbox" name="c24" id="c24" onclick="check(this, '24');">

Thanks for the answers!
BTW the discussion on ids was quite interesting

Best regards
Michal Cibor

mcibor

8:59 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had a problem concerning "this". I tried that, however I forgot that I want to place it in a clickable <tr> not just checkbox.

The code is such:

<table>
<tr onclick="check(this, '24')">
<td><input type="checkbox" name="c24" id="c24"></td><td>...</td></tr></table>

What I did instead was puting the object manually:

<tr onclick="check(document.form.c<?PHP echo $number;?>, '<?PHP echo $number;?>')">

Thank guys! That was helpful!

Best regards
Michal Cibor

Rambo Tribble

11:05 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Alternatively, you could keep your onclick tags a bit shorter by just passing the input element's id, then using the old standby, getElementById(), in the function to grab the transitory little devil's details from the heap.