Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 18-05-2009, 01:56 PM   #1 (permalink)
In The Zone
 
Sridhar_Rao's Avatar
 
Join Date: Feb 2007
Posts: 353
Default Multiple call to javascript function: how to do?


I have a form with a set of elements that repeat but with different names & ids. I use javascript validation on these elements. It is fine when I have only one set but when there are multiple sets, repeating the javascript code for each of them is a stupid idea. How can this be accomplished with a single instance of code being used for several sets of elements.

Code:
<script language="javascript">
function disp(){
	document.getElementById('t1').value = document.getElementById('s1').value;
}
</script>
Code:
<form name="abc">
<select name="s1" id="s1" onchange="disp();">
<option value="ABC">ABC</option>
<option value="CDE">CDE</option>
<option value="EFG">EFG</option>
</select>
<textarea readonly rows="2"  cols="50" name="t1" id="t1"></textarea>
</form>
This is simple. Assume, I have the same form with additional elements like this:
Code:
<form name="abc">
<select name="s1" id="s1" onchange="disp();">
<option value="ABC">ABC</option>
<option value="CDE">CDE</option>
<option value="EFG">EFG</option>
</select>
<textarea readonly rows="2"  cols="50" name="t1" id="t1"></textarea>

<select name="s2" id="s2" onchange="disp();">
<option value="ABC">ABC</option>
<option value="CDE">CDE</option>
<option value="EFG">EFG</option>
</select>
<textarea readonly rows="2"  cols="50" name="t2" id="t2"></textarea>

<select name="s3" id="s3" onchange="disp();">
<option value="ABC">ABC</option>
<option value="CDE">CDE</option>
<option value="EFG">EFG</option>
</select>
<textarea readonly rows="2"  cols="50" name="t3" id="t3"></textarea>
</form>
This is a stripped down and altered version of the form. Actual form is more complex and more elements and subfunctions.
I have a vague idea about this being achieved by using "this" reference, any help?
__________________
Want to study M.Sc in any medical subjects? Read this www.microrao.com/msc.htm
Microx, a diagnostic microbiology laboratory software application www.labmicrox.com
Sridhar_Rao is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 20-05-2009, 12:40 AM   #2 (permalink)
Broken In
 
Join Date: Aug 2008
Location: Mumbai, India
Posts: 169
Default Re: Multiple call to javascript function: how to do?

I had replied to this earlier, but seems like some problem with the forum s/w.

Trying to reproduce it again here:

~~~~
If you follow a consistent nomenclature for your selects (like s followed by a number) and similar one for the textarea objects (like t followed by the same numeral as its corresponding s object), then this might work for you:

Code:
<script language="javascript">
function disp(sParam)
{
	var num = sParam.id.substring(1, sParam.id.length);
	document.getElementById('t'+num).value = sParam.value;
}
</script>
<form name="abc">
<select name="s1" id="s1" onchange="disp(s1);">
<option value="ABC">ABC</option>
<option value="CDE">CDE</option>
<option value="EFG">EFG</option>
</select>
<textarea readonly rows="2"  cols="50" name="t1" id="t1"></textarea><BR>

<select name="s2" id="s2" onchange="disp(s2);">
<option value="ABC">ABC</option>
<option value="CDE">CDE</option>
<option value="EFG">EFG</option>
</select>
<textarea readonly rows="2"  cols="50" name="t2" id="t2"></textarea><BR>

<select name="s3" id="s3" onchange="disp(s3);">
<option value="ABC">ABC</option>
<option value="CDE">CDE</option>
<option value="EFG">EFG</option>
</select>
<textarea readonly rows="2"  cols="50" name="t3" id="t3"></textarea><BR>
</form>
~~~~
Bandu is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Sujeet
- by gforz
- by soumya

Advertisement




All times are GMT +5.5. The time now is 03:13 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2