Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing the form elements from jscript

Status
Not open for further replies.

nirmalgupta

IS-IT--Management
Sep 12, 2001
35
IN
Hi,
I am facing the problem in accessing the form element from javascript. This is because the names of the elements have "." in between. this is a sample code which gives error.
<HTML><HEAD></HEAD><BODY>
<FORM METHOD=POST ACTION=&quot;&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;jrs.somthing$text1&quot;>
<input type = button value = click onClick = fun()>
</FORM>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function fun () {
alert ( &quot;value iin text is : &quot;+ document.forms[0].jrs.somthing$text1.value );
}
</SCRIPT></BODY></HTML>

Is there any other way to access the elements from there name.or any method? id tag does not work with netscape4.5 .
rgds
Nirmal
 
Yup.. Same way you're accessing your form. If you know the index number of the element ..
Code:
function fun () {
alert ( &quot;value iin text is : &quot;+ document.forms[0].elements[0].value );
}
This would be the proper index in the example above since you have only one element.

You may still run into problems using that naming convention. If the index number of that element changes dynamically and there is no way for you to know it in your JavaScript, let me know. I have a script that will loop through all of the elements in the form and grab just the one with a specific string in the name instead of trying to access the entire name, which in your case is not working.

ToddWW
 
thanx,
actually in my program i am using this looping funda for elements. but there is a place where i don't have this option too.
is there any other way to access names and then actually using names in javascript.
rgds
Nirmal
 
Here, take a look at this.

var alertext
for (var m = 0; m < document.forms[0].elements.length; m++) {
if (document.forms[0].elements[m].name.indexOf(&quot;xyz&quot;) >= 0) {
alerttext = document.forms[0].elements[m].value
}
}
alert ( &quot;value iin text is : &quot;+ alerttext );


This loops through all of the elements in the form looking for an element name that contains a specific string. When it finds it, it will store the value of that element to alerttext which you can reference in your alert() method.

Does this make sense ??

It works well, and I use it all the time.

ToddWW :)
 
thanx yaar,
actually html page is generated by some some other software , in case of my code using jsp. now name of these elements comes from an array of java code. what i have to do is , when a perticular element comes, i have to associate a calender with that element. to associate a calender, i have to call a method of that calender,which takes that element as parameter.
the jsp code which generates this is something like this:
for (int i = 0; i < sz; i++) {
desc = (ParamInfo)params.elementAt(i);
String pmt = desc.getPrompt();
%>
<TR><TD class= clsDataLabel>
<INPUT type=&quot;text&quot; name=&quot;<%= APIConst.TAG_PARAM_PREFIX + desc.getName()%>&quot;value=&quot;<%= desc.getDefaultValue()%>&quot; id=&quot;<%= tn%>&quot;>
<!--
<% if ( (desc.getName()).indexOf (&quot;date&quot;) != -1 ) {%>
<A href=&quot;javascript:show_calendar('forms[0].<%= APIConst.TAG_PARAM_PREFIX + desc.getName()%>','DD-MON-YYYY');&quot; onmouseover=&quot;window.status='Date Picker';return true;&quot; onmouseout=&quot;window.status='';return true;&quot;>kfldfl;dlf</A><%}%> -->

now i have found out the element, but I have to pass this element in the form of
show_calendar('forms[0].<%= APIConst.TAG_PARAM_PREFIX + desc.getName()%>','DD-MON-YYYY');&quot;

now this is the problem i am facing and some how i want to pass this value to javascript function.

i think , now problem is clearer to you. please help me in this specific problem.
rgds
nirmal
 
Sure, can you just do this when generating the page.

var alertext
for (var m = 0; m < document.forms[0].elements.length; m++) {
if (document.forms[0].elements[m].name.indexOf(&quot;<%= APIConst.TAG_PARAM_PREFIX + desc.getName()%>&quot;) >= 0) {
alerttext = document.forms[0].elements[m].value
}
}
alert ( &quot;value iin text is : &quot;+ alerttext );


You're getting a little over my head here with the JSP, but you can have JSP or ASP or whatever write some or all of the JavaScript for you like I have shown above.

ToddWW :)
 
thanx ,
I have concern in this method.Page has around 60 elements, and to have all validation + calender facality, I have to run loops atleast three time. My belief is, this will hamper the performance. Am I right or wrong. please correct me.
regards
Nirmal
 
No, that will not hamper performance. JavaScript will blow through those loops before you can even count to one.

ToddWW :)
 
Hi finally i got the solution i wanted.
try this
document.forms[0].elements['nirmal.gupta'].value

this works
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top