Click to See Complete Forum and Search --> : From <Select> to Textbox


kimoo
July 2nd, 2007, 01:05 PM
Hello,

I got the following html


<select id="P_City" tabindex="9" onSelect="JCopyField()" >
<option value="1">Boston</option>
<option value="2">Ithaca</option>
<option value="3">New York</option>
</select>

<input name="" type="text" id="E_City">


What i want is when a user chooses one of the cities from the list.. it should show up in the textbox.. so i wrote the JCopyField() function as follows



Function JCopyField(){

document.T_Form.getElementById('E_City').value = document.T_Form.getElementById('P_City').value;
}



But it does not work.. can anyone take the bug out.. its driving me buts

regards,
kimoo.

PeejAvery
July 2nd, 2007, 01:45 PM
That's because it's just document.getElementById(). Don't call the form. Also, I would recommend downloading Firefox and using their JavaScript debugger. It will help you greatly.

kimoo
July 2nd, 2007, 02:23 PM
Downloaded JavaScript debugger...kind of cool.

The problem seems to be the HTML not the JavaScript.

EDIT: Accidentally edited instead of quoting.

PeejAvery
July 2nd, 2007, 03:28 PM
Your problem is the HTML. You have onselect in your select tag. Use onchange.

kimoo
July 2nd, 2007, 04:40 PM
i can not believe that i had been trying to debug such a mistake for sooo long..

Guess what even onChange does not work.. and i am still trying to find out why

gonna break my box today :$

kimoo

kimoo
July 2nd, 2007, 05:03 PM
NOOOOOOOOOOOOOO

the problem was in two things

1. onSelect had to be onChange

2. Guess this just guess the 'F' of the word function in the Javascript was UpperCase and it had to be LowerCase

OMG ~ i just love programming..

kimoo

PeejAvery
July 2nd, 2007, 05:10 PM
2. Guess this just guess the 'F' of the word function in the Javascript was UpperCase and it had to be LowerCase
What I said was correct with the onchange. Funny though, I didn't event thing about F instead of f. Sometimes you just look over things.

Waqas_Badar
July 3rd, 2007, 02:32 AM
I think to get value from select element first you to get its selected index and then from options array get its value. For example

<select name="abc" id="abc" onchange="showSelectedValue()">
<option value="A">A
<option value="B">B
<option value="C">C
</select>
function showSelectedValue()
{
var selectedIndex = document.getElementById('abc').selectedIndex;
alert(document.getElementById('abc').options[selectedIndex].value);
}

PeejAvery
July 3rd, 2007, 08:07 AM
I think to get value from select element first you to get its selected index and then from options array get its value.
You miss the point. The point was to fill the textbox when a user selects something from the drop-down. Not alter the drop-down by using JavaScript.

Anyway, the issue has already been resolved.