473,385 Members | 1,569 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,385 software developers and data experts.

calling a javascript for multiple fileds...

i want to call a javascript for onclick action on all the checkboxes on
my form.
I know that i can individually call the script for each checkbox
onclick.
I was wondering if there was any better solution.

Mar 17 '06 #1
4 1403
gr*********@gmail.com wrote:
i want to call a javascript for onclick action on all the checkboxes on
my form.


Write a javascript function that loops through all the FORM's elements,
checking if it is a 'checkbox' and executes further. Attach this
function to the onkeypress event of the FORM. That should work.

Regards,
Rithish.

Mar 17 '06 #2
gr*********@gmail.com said on 17/03/2006 1:44 PM AEST:
i want to call a javascript for onclick action on all the checkboxes on
my form.
I know that i can individually call the script for each checkbox
onclick.
I was wondering if there was any better solution.


If you want to cut down on the amount of HTML you have to send, then
attach the events using script. Then they will only be attached if the
browser supports scripting and your HTML stays cleaner.

Sample:

<form name="formA" action="">
<p>
<label for="CB1"><input type="checkbox"
id="CB1" name="CB1">CB 1</label><br>
<label for="CB2"><input type="checkbox"
id="CB2" name="CB2">CB 2</label><br>
<label for="CB3"><input type="checkbox"
id="CB3" name="CB3">CB 3</label><br>
<input type="reset">
</p>
</form>
<div id="msg">message here...</div>

<script type="text/javascript">

function addClick(fName, elType, clickFn)
{
var form = document.forms[fName];
var els;
if (form && (els = form.elements)){
for (var i=0, len=els.length; i<len; ++i){
if (els[i].type == elType){
els[i].onclick = clickFn;
}
}
}
}

function showName()
{
var el = this;
var elDiv = document.getElementById('msg');
if ( elDiv && el && el.name){
elDiv.innerHTML = el.name + ': '
+ ((el.checked)?'checked':'un&ndash;checked');
}
}

addClick('formA', 'checkbox', showName)

</script>

--
Rob
Mar 17 '06 #3
gr*********@gmail.com wrote:
i want to call a javascript for onclick action on all the checkboxes on
my form. I know that i can individually call the script for each checkbox
onclick. I was wondering if there was any better solution.


Search the archives for event bubbling. I have posted several examples
here.
PointedEars
Mar 18 '06 #4
ri*****@gmail.com wrote:
gr*********@gmail.com wrote:
i want to call a javascript for onclick action on all the checkboxes on
my form.


Write a javascript function that loops through all the FORM's elements,
checking if it is a 'checkbox' and executes further. Attach this
function to the onkeypress event of the FORM. That should work.


Event bubbling allows to access the target element through its
corresponding element object directly. No need for inefficient
loops.
PointedEars
Mar 18 '06 #5

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

Similar topics

7
by: Julia Briggs | last post by:
Hello World - I admit I'm new to javascript but I've tried for days to find a solution to my problem. Basically I have 3 unique javascript files that do different screen display events that I...
0
by: allyn44 | last post by:
HI--I have 2 tables Cut: cut ID, HistNumb, Block, date: Cut Id is the primary key, the other 3 fileds are indexed to be unique Slides: Cutid SlideID, and various other fields: there can be...
1
by: Ivan Carey | last post by:
How can a query display multiple fields with diferent condition on the same field example I have a field name of reason and a field name of duration. I would like to display 2 fileds of total...
2
by: mukeshhtrivedi | last post by:
I have Cost Analysis form created where there are three fileds and I want to have total of all the fileds at the end. I have created text box which has Control Source as =++. It shows me total of...
8
by: Tradeorganizer | last post by:
Hi, I have a database with table name as test in that i have 6 colums they are name varchar (20) address varchar (20) position varchar (20) nametype1 varchar (20) nametype2 varchar (20)
1
by: Memphis Steve | last post by:
Is it possible to combine multiple javascipts into one file and then call that file from a linked URL in the head section of an XHTML file? Here are the two scripts I want to use with the...
15
by: Sampat | last post by:
Hi, I wanted to know the performance of calling a function pointer v/s a normal function call in javascript in a scenario where there are multiple calls in the js to the same function. Please...
1
by: pavankumar106 | last post by:
hi i need the javascript function for the below requirement? im having a DATALIST in da itemtemplate im having fileds like noofiimages in a textbox and price of that image in a label.here the images...
2
by: infernodeep | last post by:
hi guys,im new to this forum could anyone help me to solve this problem for me.. In my site im generating multiple text boxes and i need to wirte validations for that.... <form id="form1"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.