473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

childnodes to attach function

Hi Folk

I want to add an "onClick" function to the radio boxes, but I am
having trouble. Can you help me

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>leetMachines</title>
<style>
body {width: 600px; margin-left: auto; margin-right: auto;}
label {display: block;}
h2 {color: green;}
p.moreinfo {float: right;}
d.options {clear: right;}
h2, p, div {margin: 0px; padding: 0px;}
</style>
<script type="text/javascript">
function starter(elname) {
obj = document.getElementById(elname);
addclicks(obj);
}
function addclicks() {//gets all variables from a form
for (i=0; i < obj.childNodes.length; i++) {
var newobj = obj.childNodes[i];
tgname = newobj.tagName
if(tgname) {
if (tgname == "INPUT" || tgname == "input") {
newObj.onClick = gonow;
}
}
addclicks(newobj);
}
}

function gonow() {
alert("click");
}

</script>
</head>
<body onload="starter('former');">
<form method="post" action="" name="former" id="former" onclick="">
<h2>Choose below</h2>
<div class="options" id="item1-options">
<label for="item1"><input type="radio" name="item1" value="0"
id="item1-option0" />option 1</label>
<label for="item1"><input type="radio" name="item1" value="1"
id="item1-option1" />option 2</label>
<label for="item1"><input type="radio" name="item1" value="2"
id="item1-option2" />option 3</label>
</div>
</form>
</body>
</html>

Mar 4 '07 #1
2 1435
On Mar 5, 7:53 am, "windandwaves" <nfranc...@gmail.comwrote:
Hi Folk

I want to add an "onClick" function to the radio boxes, but I am
having trouble. Can you help me

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
There have been many long discussions on whether pages should use
XHTML or HTML, none have shown any significant benefit to using XHTML
on the web. The disadvantages are great, just use HTML unless you
have a really good reason to use XHTML.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>leetMachines</title>
<style>
body {width: 600px; margin-left: auto; margin-right: auto;}
label {display: block;}
h2 {color: green;}
p.moreinfo {float: right;}
d.options {clear: right;}
h2, p, div {margin: 0px; padding: 0px;}
</style>
<script type="text/javascript">
function starter(elname) {
obj = document.getElementById(elname);
Better to keep variables local with 'var':

var obj = document.getElementById(elname);

addclicks(obj);

}

function addclicks() {//gets all variables from a form
You pass a reference to obj when you call the function, but don't
assign it to a local variable - you depend on its being a global
variable. Much better to give it a local variable:

function addclicks(obj) {

for (i=0; i < obj.childNodes.length; i++) {
Counters should never be allowed to become global:

for (var i=0, len=obj.childNodes.length; i<len; i++) {
var newobj = obj.childNodes[i];
tgname = newobj.tagName
var tgname = newobj.tagName

if(tgname) {
if (tgname == "INPUT" || tgname == "input") {
Usually:

if (tgname.toLowerCase() == 'input') {

newObj.onClick = gonow;
}
}
addclicks(newobj);
This will recursively call addclicks. An input can't have any
children, so it's pointless.

If you only want to add onclick's to inputs, why not use
getElementsByTagName?

function addclicks(obj){
var nodes = obj.getElementsByTagName('input');
var i = nodes.length;
while (i--){
nodes[i].onclick = gonow;
}
}
--
Rob

Mar 4 '07 #2
On Mar 5, 11:51 am, "RobG" <r...@iinet.net.auwrote:
On Mar 5, 7:53 am, "windandwaves" <nfranc...@gmail.comwrote:
Hi Folk
I want to add an "onClick" function to the radio boxes, but I am
having trouble. Can you help me
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

There have been many long discussions on whether pages should use
XHTML or HTML, none have shown any significant benefit to using XHTML
on the web. The disadvantages are great, just use HTML unless you
have a really good reason to use XHTML.


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>leetMachines</title>
<style>
body {width: 600px; margin-left: auto; margin-right: auto;}
label {display: block;}
h2 {color: green;}
p.moreinfo {float: right;}
d.options {clear: right;}
h2, p, div {margin: 0px; padding: 0px;}
</style>
<script type="text/javascript">
function starter(elname) {
obj = document.getElementById(elname);

Better to keep variables local with 'var':

var obj = document.getElementById(elname);
addclicks(obj);
}
function addclicks() {//gets all variables from a form

You pass a reference to obj when you call the function, but don't
assign it to a local variable - you depend on its being a global
variable. Much better to give it a local variable:

function addclicks(obj) {
for (i=0; i < obj.childNodes.length; i++) {

Counters should never be allowed to become global:

for (var i=0, len=obj.childNodes.length; i<len; i++) {
var newobj = obj.childNodes[i];
tgname = newobj.tagName

var tgname = newobj.tagName
if(tgname) {
if (tgname == "INPUT" || tgname == "input") {

Usually:

if (tgname.toLowerCase() == 'input') {
newObj.onClick = gonow;
}
}
addclicks(newobj);

This will recursively call addclicks. An input can't have any
children, so it's pointless.

If you only want to add onclick's to inputs, why not use
getElementsByTagName?

function addclicks(obj){
var nodes = obj.getElementsByTagName('input');
var i = nodes.length;
while (i--){
nodes[i].onclick = gonow;
}
}

--
Rob- Hide quoted text -

- Show quoted text -
Hi Rob

You are a legend. Amazing!

Cheers, Nicolaas

Mar 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Michael | last post by:
Hello everybody, Newbie here. I've been working on this for the last two days and I can't figure out where this problem is. I think it's something so obvious, but I can't see it! OK, firstly...
4
by: Gagan Diesh | last post by:
all I want to do is add the ability for an image to take the user further down a page (href="#bottom"). Anyone tell me how to do this? ==the code below works perfectly=== My code on an...
2
by: chuck | last post by:
Hi, I am modifying some code from here http://www.quirksmode.org/dom/domform.html I have a div 'readroot' that I clone. I change the change the id and name of the childnodes of 'readroot' to...
5
by: john.teixeira | last post by:
Hey all, Sorry if this is a newbie question, but does javascript have a built-in function that will take a string, parse any HTML tags from the string and return back a DOM element representing...
3
by: samuelberthelot | last post by:
Hi, I'm trying to write a recursive fucntion that takes as parameters a html div and an id. I have to recurse through all the children and sub-children of the div and find the one that matches the...
10
by: Rik | last post by:
Hi all, I usually don't really use javascript, but for a pretty big form, I'm trying the following: I've got arbitrarily deep nested unorderd list, with in every <li3 checkboxes. It's for...
2
by: tmartiro | last post by:
Hi guys, I want to create link attach function like it implemented in facebook or google buzz. Do you know how to create this kind of stuff? Thanks in advance
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.