childnodes to attach function

W

windandwaves

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;
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>
 
R

RobG

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;
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.onclick = gonow;
}
}
 
W

windandwaves

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;
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.onclick = gonow;
}
}


Hi Rob

You are a legend. Amazing!

Cheers, Nicolaas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top