0

I tried searching for an answer to this for a while, but to no avail.

I'm trying to do a simple online calculator (that calculates some photovoltaic panels energy), but I'm stuck in something simple (I'm new to Javascript although I worked with Flash's ActionScript 3.0 for a while).

What I need done is a html select that defines which other select group appears in the page. Something like this (obviously this doesn't work, just setting an example):

HTML

<html>
<body>
<select id="test1" onclick="checkField()">
<option>Selected A Group</option>
<option>Selected B Group</option>
</select>
<script>//insert second group here</script>
</body>
</html>

Javascript

function checkField(){
var temp = document.getElementById('test1').value;

if(temp === "Selected A Group"){
//insert code to "echo" the first optional select group
} else {
//insert code to "echo" the second optional select group
}
}

Sorry if its a bit confusing, but I cant really explain all that well. Here is an example of what I would want, where selecting a option makes the other fields change accordingly: http://www.toshiba.eu/innovation/download_drivers_bios.jsp

2
  • Your code works, what does this mean insert code to "echo" the first optional select group Commented Oct 18, 2013 at 10:19
  • have a look to my answer, even if you have chosen the definetive one already, it contains some practices that can help you understand Javascript better Commented Oct 18, 2013 at 11:05

3 Answers 3

1

you are almost there, actually javascript doesn't "echo" values directly, it does log values using console.log(your value); to a debug console, similar to AS2 trace() if my memory isn't failing.

To "output" information to the document you should have a look into document.write When you use document.write it will directly write to the documents end.

The "correct" way would be to create a DOM element, with the elements you want inside it, and then append it to the desired element. Have a look at the comments

<!-- Be Aware to use the onchange trigger on select boxes, if you use onclick the function will run, even
if you didn't really chose any option -->
<select id="test1" onchange="checkField()">
<!-- Is good to have a first non-value option, better to trigger the onchange event, if you have 
Select A Group as first option and you click on it, it didn't really "Change", you would have to
pick B Group and then A Group again to trigger the onchange event correctly. -->
<option value="">-- select an option --</option>
<!-- You can have a value attribute on the options, so it's easy to process when programming
while displaying a more detailed description to the users -->
<option value="A">Selected A Group</option>
<option value="B">Selected B Group</option>
</select>

<!-- We create an empty element where we are gonna place the new Select -->
<div id="newSelect"></div>

<!-- By Placing the Javascript on the end of <body>, we ensure that all the DOM elements loaded before running the script -->
<script>
    function checkField(){
        var newSelect = document.getElementById('newSelect'); //targeting container;    
        var temp = document.getElementById('test1').value;

        //Some tasks we do always the option chose is not the first custom one, so we don't have to repeat it
        //on the two If's below
        if(temp !== ""){
            // We remove the select if we placed one already before, so we can add the new one,
            // For example if we chose B Group but changed our mind and Chose A Group later.
            if(oldChild = newSelect.getElementsByTagName('select')[0]){
                oldChild.remove();
            }

            var select = document.createElement("select");
            select.setAttribute('id', 'newSelect');

        }

        if(temp === "A"){
            //you could do JUST:
            //body.innerHTML = "all the html you want in here" instead of all the code following;
            //but all those code is supposed to be the "correct way" of adding elements to the HTML,
            //Google a bit about that for detailed explanations

            var option1 = document.createElement("option");
            option1.value = 1;
            option1.text = "Option 1";
            var option2 = document.createElement("option");
            option1.value = 2;
            option2.text = "Option 2";

            select.appendChild(option1);
            select.appendChild(option2);

            newSelect.appendChild(select);
        } else {
            var option1 = document.createElement("option");
            option1.value = 3;
            option1.text = "Option 3";
            var option2 = document.createElement("option");
            option1.value = 4;
            option2.text = "Option 4";

            select.appendChild(option1);
            select.appendChild(option2);

            newSelect.appendChild(select);
        }
    }
</script>

Of course there are ways to make this slightly shorter, using loops if your data to ouput has a pattern, but lets do it the "simple" way so you get a grasp of Javascript.

Hope all this helped you!!

Sign up to request clarification or add additional context in comments.

Comments

0

Demo here: http://jsfiddle.net/3GkrX/

Just about the same as Mevins.... changed to switch/case though

html:

<select id="test1" id="name" onchange="checkField()">
    <option>----</option>
<option>Selected A Group</option>
<option>Selected B Group</option>
</select>
<div id="optional">Please select!</div>

JS:

function checkField(){
var temp = document.getElementById('test1').value;

    switch(temp){

        case "Selected A Group":
            document.getElementById("optional").innerHTML="<select name='optionalA'><option>1</option><option>2</option></select>";
            break;
        case "Selected B Group":
            document.getElementById("optional").innerHTML="<select name='optionalB'><option>3</option><option>4</option></select>";
            break
            default:
                document.getElementById("optional").innerHTML="Please select!";
            break;

    }

}

Also added the second group as a real option, and a default as "please select". may or may not be necessary in your case

1 Comment

Ah, I did not know about the "innerHTML". In Flash AS3, I just add/remove movieclips to the stage so, being a newcomer to JS, sometimes I don't know how to affect the html from within the JS. This worked perfectly, thank you.
0

Here is the demo http://codepen.io/anon/pen/izAHo your doing it almost right.

You should put the onclick event on the option tag to trigger changes based on the option selected.

HTML

<html>
<body>

<select id="test1">
<option onclick="checkField()">Selected A Group</option>
<option>Selected B Group</option>
</select>

<select id="test2">
<option  onclick="check2Field()">Selected C Group</option>
<option>Selected D Group</option>
</select>

<script>//insert second group here</script>
</body>
</html>

JS

function checkField(){
var temp = document.getElementById('test1').value;
if(temp === "Selected A Group"){
document.getElementById('test2').innerHTML="<option>Selected halloooo Group</option>";
} else {
//insert code to "echo" the second optional select group
}
}

Check out my demo for more clarity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.