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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I'm new to javascript and I

Status
Not open for further replies.

DonBerry

Technical User
Jun 28, 2001
26
0
0
US
Hi,

I'm new to javascript and I am having trouble with a function to set the value of a text type input in a form. The problem I'm having is the name of the text input is in a variable and I don't know how to use that variable correctly in the access sting ie
document.myform.mytext(this is in a variable).value=whatever
Can someone point me in the right direction?
TIA,
Don
 
document.getElementById(this is in a variable)
document.getElementsbyName(this is in a variable)
 
Thanks for the reply it may help but thats for tomarrow! but I posted my question incorrectly what I really ment and didn't type is this:
I'm new to javascript and I am having trouble with a function to get the value of a text-type input in a form. The problem I'm having is the name of the text input is in a variable and I don't know how to use that variable correctly in the access sting ie
whatever=document.myform.mytext(the name mytext is in a variable).value
Can someone point me in the right direction?
TIA,
Don
 
Well let me try one more time its really late here!
I am having trouble with a javascript function to get the value of a text-type input in a form. The problem I'm having is the "name" of the text input is in a variable this same function retreived from another eliment of the form and I don't know how to use that variable correctly in the access sting ie
whatever=document.myform.mytext(the "name" mytext is in a variable).value
Can someone point me in the right direction?
TIA,
Don
 
Try this. I'm assuming that your text boxes are generated from an array, so this should tie in quite well with what you've already done.


<script>
function check(n){
alert(document.myform.elements[n].value)
}
</script>

<form name=myform>
Text Box 1: <input type=&quot;text&quot; name=&quot;test1&quot; value=&quot;Eeny&quot;><br>
Text Box 2: <input type=&quot;text&quot; name=&quot;test2&quot; value=&quot;Meeny&quot;><br>
Text Box 3: <input type=&quot;text&quot; name=&quot;test3&quot; value=&quot;Miney&quot;><br>
Text Box 4: <input type=&quot;text&quot; name=&quot;test4&quot; value=&quot;Mo&quot;><br>
</form><br>

<input type=&quot;button&quot; value=&quot;What's in Text Box 1?&quot; onclick=&quot;check(0)&quot;><br>
<input type=&quot;button&quot; value=&quot;What's in Text Box 2?&quot; onclick=&quot;check(1)&quot;><br>
<input type=&quot;button&quot; value=&quot;What's in Text Box 3?&quot; onclick=&quot;check(2)&quot;><br>
<input type=&quot;button&quot; value=&quot;What's in Text Box 4?&quot; onclick=&quot;check(3)&quot;><br>

Phil
 
getElementById will retreive one element (textbox or select or div or whatever element has the id)
getElementsByName retreives an array of elements.
Here is some sample code:
<script>
function getValById(strID){
objectTextBox = document.getElementById(strID);
alert(objectTextBox.value)
}
function getValByName(strName){
objectTextBox = document.getElementsByName(strName);
var i = 0;
while(i<objectTextBox.length){
alert(objectTextBox[ i ].value);
i++;
}
}
</script>
<input type=text id='mytextID' name='mytextName' value=&quot;hope you get it now&quot;>
<input type=text id='mytext2ID' name='mytextName' value=&quot;it's late maybe you should get some sleep&quot;>
<input type=button value=&quot;get value of the text by id&quot; onclick=getValById('mytextID')>
<input type=button value=&quot;get value of the text by name&quot; onclick=&quot;getValByName('mytextName');&quot;>
 
<script type=&quot;text/javascript&quot;>
<!--Javascript to Handle OnChange Event of Select Box
function getprintid()
{
option=document.forms.artcart.printstyle.options[document.forms.artcart.printstyle.selectedIndex].value
namehidden=option
//price=document.artcart.[namehidden].value
document.forms.artcart.PrintPrice.value=namehidden
}
//-->
</script>

The above is the code I'm working with. It is some code I got off a javascript site and I'm trying to modify to fit my needs, probably not the best approach but like I said I know almost nothing about java script. printstyle is an option box populated by some ASP code. The &quot;value&quot; of a selected object is a numeric text string that points to a specific artist's print in a data base. At the same time the select is populated, a group of hidden inputs are created with a name = to that option's &quot;value&quot; and a &quot;value&quot; = to the print's price. When the selection changes in the option box, my function is called and retrieves the &quot;value&quot; of the current selection in varible namehidden which is also the name of a hidden input I want to extract the price from and disply in a text input named PrintPrice. The code as shown will dispay the &quot;value&quot; of the select option in PrintPrice fine but when I try to add the step that is commented out and change namehidden in the last line of code to price, I get nothing in my PrintPrice text box.

Don
 
function getprintid()
{
option=document.forms.artcart.printstyle.options[document.forms.artcart.printstyle.selectedIndex].value
namehidden=option
price=document.getElementById(namehidden).value
//or
price=document.getElementsByName[0].value
document.forms.artcart.PrintPrice.value=namehidden
}
//-->
</script>

 
harmmeijer,

Thanks your code works for me. I'm not sure I totally understand it but as long as it works!! For some reason I was under the impression the name property and the id property were totally different but that seems not to be the case. When I get some time, I guess I'll have to try to get up to speed on javascript. I used your first example. In the second example would the 0 in the brackets have to be changed to represent the number of the form you're trying to access (presuming you'd have a clue what that was in a page with numerous forms). Just curious.

Don
 
The id of an object should be unique, the name of an object does not have to be unique. So getElementById should return 1 element and getElementsByName returns an array of elements.

Actually if you have 2 elements with the same id (not wise but it's possible) getElementById will allso return an array of elements.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top