Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Simple Javascript Form Populate Issue

civicstiil

New Coder
Would someone kindly help me with the below? Built this simple javacsript form a few years ago and recently stopped working once my firm updated browsers. Can't figure this one out! When selecting the "team" - it's supposed to populate the mascot and the color. The mascot populates but color says "undefined". Hopefully an easy fix!

HTML:
<HTML>
<HEAD>
<script>
<!--
function populate(item){
for(var i=0;i<item.form.elements.length;i++){
if(item==item.form.elements[i]){
item.form.elements[i+1].value=
item.options[item.selectedIndex].value;
}
}
for(var i=0;i<item.form.elements.length;i++){
if(item==item.form.elements[i]){
item.form.elements[i+2].value=
item.options[item.selectedIndex].value2;
}
}
}
// -->
</script>
</HEAD>
<BODY>
<form>
Team: <select onchange="populate(this)"><option value="" value2="">--- Select Account ---</option>

<option value="Billy" value2="Blue">Bluebirds</option>
<option value="Hal" value2="Brown">Owls</option>
<option value="Tony" value2="Red">Hawks</option>

</Select>
<BR>
Mascot:  <input type="text" /readonly>
<BR>
Color:  <input type="text" /readonly>
</form>
</BODY>
</HTML>
 
Last edited by a moderator:
It says undefined, because HTMLOptionElement does not have "value2" property, which you try to read in the second loop (item.form.elements[i+2].value=item.options[item.selectedIndex].value2;). Replacing it with item.form.elements[i+2].value=item.options[item.selectedIndex].value; should fix the problem. Also, html tags are usually written in lowecase and html comments inside script tags are not recommended. Hopefully this fixes it!
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom