cancel
Showing results for 
Search instead for 
Did you mean: 

document.form.element.value

Former Member
0 Kudos

Hi All,

I'm very new to javascript and had a quick question! Her is a sample of my code.

<script language="JavaScript" type="text/javascript">

function fnShowText(txt)

{

var x = document.getElementById(txt);

var e = document.getElementById(txt).name;

var sText = x.options.value;

var f = e.concat("b");

for (i=0; i<mainform.elements.length; i++)

if (mainform.elements<i>.name == f)

{

document.forms[0].elements<i>.value = sText;

}

}

</script>

I know what the value of elements is, but I want this script to be dynamic. Instead of looping through all the elements to find the value 'i' like I am currently doing it. This is bad response time. Is there a way to skip the looping and make this call dynamically if I already have the name of the element?

Thanks in advance!

Eric

By the way there is a 'i' after elements but for some reason it isn't showing up on this board.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

You can also give selects names and id and reference them just like a text field.

<script>

function setText(){

document.getElementById("t1").value =

document.getElementById("s1").options[document.getElementById("s1").selectedIndex].innerText

}

</script>

<select name=s1" id="s1" onchange="setText();">

<option value=1>one</option>

<option value=2>two</option>

<option value=3>three</option>

</select><br>

<input type=text name="t1" id="t1"></input>

Answers (2)

Answers (2)

Former Member
0 Kudos

It is not quite clear on what you are doing. Are you trying to set a text field with the text or value of a select?

Former Member
0 Kudos

That is correct!

I'm trying to pass the value of a html select box to a htmlb input field using java script. I have multiple select boxes and each one is assigned to its own htmlb input field. I only want to use on java script to a transfer the values. That is why I need to dynamically assign the name of the select box to get the value and dynamically assign it to the input box with its name.

Thanks,

Eric

Former Member
0 Kudos

if you have a input tag in your html page you can go about it the following ways.

<input type=text name="t1" id="t1"></input>

<script>

document.getElementById("t1").value = "here";

document.getELementsByName("t1")[0].value = "there";

</script>

getElementsByName brings back an array.

getElementByID gets back one object.

You should test your taget browsers with both of these. I know that IE works fine with getID, but others do not.