Forum Moderators: open
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
[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.
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
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.
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>
Section 7.6 of ECMA-262 would tend to support this interpretation (all puns intended).
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();
}
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
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