/    Sign up×
Community /Pin to ProfileBookmark

How to display values from two select forms

Hi Guys,

I have been trying to figure out how to display values from these two select forms, all to no avail. I’m just a novice in coding anyways. Your help is highly appreciated..

Below is what I have done to get it working but no luck…

<script type=”text/javascript”>
function firstResult()
{
var x=document.getElementById(“first”).selectedIndex;
//alert(document.getElementsByTagName(“option”)[x].value);
var objHidden = document.getElementById(“frstHidden”);
objHidden.value = document.getElementsByTagName(“option”)[x].textContent;
}
function secondResult()
{
var y=document.getElementById(“second”).selectedIndex;
//alert(document.getElementsByTagName(“option”)[y].value);
var obj2Hidden = document.getElementById(“scndHidden”);
obj2Hidden.value = document.getElementsByTagName(“option”)[y].textContent;
}

</script>

<!– form 1 –>
<form>

<select name=”amount” style=”width:250px;font:9px;margin-top:4px” id=”first” onchange=”firstResult()”>
<option>–Select Course–</option>
<option value=”a”>A</option>
<option value=”b”>B</option>

</select>
<input type=”text” name=”” id=”frstHidden” value=””/>
</form>
<!– end of form 1–>

<!– form 1 –>
<form>

<select name=”amount” style=”width:250px;font:9px;margin-top:4px” id=”second” onchange=”secondResult()”>
<option>–Select Course–</option>
<option value=”c”>C</option>
<option value=”d”>D</option>

</select>
<input type=”text” name=”” id=”scndHidden” value=””/>
</form>
<!– end of form 2->

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@benconnektauthorApr 27.2012 — I need your help please...professional coders.. the forms work well on seperate pages but would'nt work on same page. What am I doing wrong?

I need the select objects to be passed to the input boxes respectively...
Copy linkTweet thisAlerts:
@stevengohApr 27.2012 — It seem to me that the second form is not working correctly. The first one is all right.
Copy linkTweet thisAlerts:
@benconnektauthorApr 27.2012 — yes stev, the second form would work on a seperate page but wouldn't work on same page with the first one....
Copy linkTweet thisAlerts:
@slaughtersApr 27.2012 — Both selects have the same name. Try changing one.
Copy linkTweet thisAlerts:
@benconnektauthorApr 27.2012 — I have done that already..changed the select names to amount1 and amount2..and no help at all
Copy linkTweet thisAlerts:
@masterwinApr 27.2012 — Hi,

Add Name to 2nd form and change 2nd function with form name

<script type="text/javascript">

function firstResult()

{

var x=document.getElementById("first").selectedIndex;

//alert(document.getElementsByTagName("option")[x].value);

var objHidden = document.getElementById("frstHidden");

objHidden.value = document.getElementsByTagName("option")[x].textContent;

}

function secondResult()

{

var y=document.getElementById("second").selectedIndex;

//alert(document.secFrm.getElementsByTagName("option")[y].value);

var obj2Hidden = document.getElementById("scndHidden");

obj2Hidden.value = document[B].secFrm.[/B]getElementsByTagName("option")[y].textContent;

}

</script>

<!-- form 1 -->

<form>



<select name="amount" style="width:250px;font:9px;margin-top:4px" id="first" onChange="firstResult()">

<option>--Select Course--</option>

<option value="a">A</option>

<option value="b">B</option>

</select>

<input type="text" name="" id="frstHidden" value=""/>

</form>

<!-- end of form 1-->

<!-- form 1 -->

<form [B]name="secFrm"[/B]>



<select name="amount" style="width:250px;font:9px;margin-top:4px" id="second" onChange="secondResult()">

<option>--Select Course--</option>

<option value="c">C</option>

<option value="d">D</option>

</select>

<input type="text" name="" id="scndHidden" value=""/>

</form>

<!-- end of form 2->>
Copy linkTweet thisAlerts:
@benconnektauthorApr 27.2012 — Wow great. Thanks a lot for this solution. However, since I was not getting any replies, I managed to sort it out using Jquery:

<script>

$("#first").change(function () {

var str = "";

$("#first option:selected").each(function () {

str += $(this).text() + " ";

});

$("#frstHidden").val(str);

})

.change();

$("#second").change(function () {
var str = "";
$("#second option:selected").each(function () {
str += $(this).text() + " ";
});
$("#scndHidden").val(str);
})
.change();

</script>

Thanks again for taking the time to fix the bug.
Copy linkTweet thisAlerts:
@masterwinApr 30.2012 — Hi benconnekt

thanks ..its good to use jquery

Thanks

masterwin
×

Success!

Help @benconnekt 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.18,
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,
)...