/    Sign up×
Community /Pin to ProfileBookmark

drop down list get value problem….

Hi, I’m new with Javascript and I have the following problem,
I use the Select() function to retrive and display in alert box
the index and the text value of a drop down element option.
My problem is that the t_index and the t_value variables seem to both store the index value, even though I want to store the text value for the second one.
Could someone provide me with some idea of what I am doing wrong..?
Many thanks in advance……

//************************************************

function Select() {
alert(“Passed_1. Entered the function…”);

var t_index = document.activeElement.value;
alert(“Passed_2. Value of t_index is: ” + t_index);

var listBox = document.getElementById(“main”).Make;
var t_value = (listBox.options[t_index].value);

alert(“Passed_3. The value of val is: ” + t_value);
}
// End –>
</script>

</HEAD>

<!– STEP TWO: Copy this code into the BODY of your HTML document –>

<BODY>

<center>

<FORM NAME=”main”>
<SELECT NAME=”Make” onChange=”Select()”>
<OPTION VALUE=”-1″>Select a Test
<OPTION VALUE=1>test_1
<OPTION VALUE=2>test_2
<OPTION VALUE=3>test_3
<OPTION VALUE=4>test_4
</SELECT>
<BR>
<SELECT NAME=”Result” >

</SELECT>
</FORM>

</center>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@David_HarrisonJan 23.2004 — This should work:

<i>
</i>
&lt;script type="text/javascript"&gt;&lt;!--

function doselect(sel) {
alert("Passed_1. Entered the function...");

var t_index = sel.selectedIndex;
alert("Passed_2. Value of t_index is: " + t_index);

var t_value = (sel.options[t_index].value);
alert("Passed_3. The value of val is: " + t_value);
}

//--&gt;&lt;/script&gt;

&lt;/head&gt;

&lt;body style="text-align:center;"&gt;

&lt;form name="main"&gt;

&lt;select name="make" onchange="doselect(this)"&gt;
&lt;option value="-1"&gt;Select a Test&lt;/option&gt;
&lt;option value=1&gt;test_1&lt;/option&gt;
&lt;option value=2&gt;test_2&lt;/option&gt;
&lt;option value=3&gt;test_3&lt;/option&gt;
&lt;option value=4&gt;test_4&lt;/option&gt;
&lt;/select&gt;&lt;br&gt;

&lt;select name="result"&gt;
&lt;option&gt;&lt;/option&gt;
&lt;/select&gt;

&lt;/form&gt;

×

Success!

Help @mvas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...