473,770 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>leetMach ines</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.getEle mentById(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(newob j);
}
}

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"><in put type="radio" name="item1" value="0"
id="item1-option0" />option 1</label>
<label for="item1"><in put type="radio" name="item1" value="1"
id="item1-option1" />option 2</label>
<label for="item1"><in put type="radio" name="item1" value="2"
id="item1-option2" />option 3</label>
</div>
</form>
</body>
</html>

Mar 4 '07 #1
2 1457
On Mar 5, 7:53 am, "windandwav es" <nfranc...@gmai l.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.dt d">
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>leetMach ines</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.getEle mentById(elname );
Better to keep variables local with 'var':

var obj = document.getEle mentById(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.childNo des.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.toLower Case() == 'input') {

newObj.onClick = gonow;
}
}
addclicks(newob j);
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
getElementsByTa gName?

function addclicks(obj){
var nodes = obj.getElements ByTagName('inpu t');
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, "windandwav es" <nfranc...@gmai l.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.dt d">

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>leetMach ines</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.getEle mentById(elname );

Better to keep variables local with 'var':

var obj = document.getEle mentById(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.childNo des.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.toLower Case() == 'input') {
newObj.onClick = gonow;
}
}
addclicks(newob j);

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
getElementsByTa gName?

function addclicks(obj){
var nodes = obj.getElements ByTagName('inpu t');
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
1954
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 in a very simple page (please see www.intomobiles.com/test3.htm - it just displays the code that i use in an asp page) I call a function "processForm". The actual form is described in an asp page and is SSI linked to the page (for the full...
4
2696
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 image: <a
2
2568
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 the original name plus a number(counter). The problem is I have i have a div 'serials' inside 'readroot' and the childnodes of
5
6153
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 the root of the HTML tree represented by the string? For example is I called HTML2DOM('<strong>foo</strong>''), it would return the 'strong' element with one text element child with the value of 'foo'. Thanks,
3
2959
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 id. I have the following but it doesn't work well (algorighm issue) : var cn = null; function getChildElement(parent, childID){ for (var i=0; i<parent.childNodes.length; i++){ cn = parent.childNodes;
10
4291
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 granting users certain rights, and rights are inherited (or unset) downwards. To directly illustrate what certain changes will do for the user, I want to
2
1760
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
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.