0

I am trying to use values of options of dropdown menu in my loop, but i get uncaught error, all is undefined in filter1.

function filter1() {
    var x = document.getElementById("filter1").value;
    switch(x) {
        case all: 
        getElementsByClassName("1").style.display="none";
        break;
    } 
    console.log(x);
}

HTML:

 <select id="filter1" name="country" onchange="filter1()">
          <option value="all">All countries</option>
 </select>
3
  • 1
    case "all" Notice the quotes . Commented Jun 28, 2017 at 12:53
  • @abhishekkannojia Can I add this as an answer. I can mention you if you would like Commented Jun 28, 2017 at 12:54
  • @abhishekkannojia thanks Commented Jun 28, 2017 at 12:54

4 Answers 4

1

You are missing the quotes to assign string in case 'all'.And add one option greater then only perform the change function

Change document.getElementsByClassName("1")[0] instead of getElementsByClassName("1") .Because the classname is the multiple selector

function filter1() {
  var x = document.getElementById("filter1").value;
  switch (x) {
    case 'all':
     console.log('work')
      //document.getElementsByClassName("1")[0].style.display = "none";
      break;
  }
  console.log(x);
}
<select id="filter1" name="country" onchange="filter1()">
          <option value="all">All countries</option>
          <option value="one">All one</option>
 </select>

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

Comments

0

Use This instead

function filter1() {
    var x = document.getElementById("filter1").value;
    switch(x) {
        case "all": 
        getElementsByClassName("1").style.display="none";
        break;
    } 
    console.log(x);
}

Comments

0

Short answer: you forgot the quotes surrounding 'all'.

This should work:

function filter1(value) {
    switch(value) {
        case 'all': 
        //getElementsByClassName("1").style.display="none";
        console.log('all selected');
        break;
    } 
    console.log(value);
}
<select id="filter1" name="country" onchange="filter1(this.value)">
    <option value="all">All countries</option>
    <option value="us">US</option>
</select>

Comments

0

may be you should use the next construction: function filter1(){ return document.getElementById("filter1").getAttributes("value").then(function(){ }) }

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.