4

I have a question using javascript to show hide a div when using a dropdown. Code works for links and buttons but im asking if there's any way to rewrite it so it can use the SELECT option. Like if i select 'Show' from dropdown it will show me the div containing 'Hello world!'

My current Javascript:

<script>
function showMe(id) {
    var e = document.getElementById(id);
    if(e.style.display == "block") {
        e.style.display = "none";
    } else {
        e.style.display = "block";
    }
}
</script>

And the index.html contains:

<select>
    <option>Hide</option>
    <option onselect="showMe('idShowMe')">Show</option>
</select>

<div id="idShowMe" style="display: none">
    <b>Hello world!</b>
</div>
2
  • 1
    onselect is not implemented on option elements, you need to attach onchange to select element. Commented May 6, 2015 at 15:30
  • Similar question here. Commented Mar 27, 2017 at 13:03

4 Answers 4

9

You can change your code

function showMe(e) {
    var strdisplay = e.options[e.selectedIndex].value;
    var e = document.getElementById("idShowMe");
    if(strdisplay == "Hide") {
        e.style.display = "none";
    } else {
        e.style.display = "block";
    }
}
<select onchange="showMe(this);">
    <option>Hide</option>
    <option>Show</option>
</select>

<div id="idShowMe" style="display: none">
    <b>Hello world!</b>
</div>

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

1 Comment

Extra question - can i also modify the code in any way so i can use diff selections to show/hide diff divs? Like 1 selecting makes 1 div visible and choosing another one blocks the first one and makes the other visible?
2
    <select onchange="showMe(this)">
    <option>Hide</option>
    <option >Show</option>
</select>

<div id="idShowMe" style="display: none">
    <b>Hello world!</b>
</div>



function showMe(selectedOption) {

    if(selectedOption.value=="Hide") {
        document.getElementById('idShowMe').style.display = 'none';
    } else {
        document.getElementById('idShowMe').style.display = 'block';
    }
}

Comments

2

JS Fiddle for the same

<select id="mySelect" onchange="showMe('idShowMe')">
    <option>Hide</option>
    <option>Show</option>
</select>

<div id="idShowMe" style="display: none">
    <b>Hello world!</b>
</div>

<script>
function showMe(id) {
    var e = document.getElementById(id);
    if(e.style.display == "block") {
        e.style.display = "none";
    } else {
        e.style.display = "block";
    }
}
</script>

Comments

1

This can be done with onchange event and then checking the selected option. http://jsfiddle.net/hanno_drefke/dwfr224q/

the Javascript code:

var current;
function showMe(element) {
  if(current!==undefined){
    document.getElementById(current).setAttribute('style','display:none');
  }
  var fetchMe = element.options[element.selectedIndex].getAttribute('data-show');
  if(fetchMe!==null){
    document.getElementById(fetchMe).setAttribute('style','display:block'); 
    current=fetchMe;
  }
}

and minimal change ot your HTML Code:

<select onchange="showMe(this)">
    <option>Hide</option>
    <option data-show="idShowMe">Show</option>
</select>

<div id="idShowMe" style="display: none">
    <b>Hello world!</b>
</div>

With this solution are free to connect any layer with the corresponding id to the option. Just insert the data-show value and create a div with the id: http://jsfiddle.net/hanno_drefke/dwfr224q/1/

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.