最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I add an event listener to a select element? - Stack Overflow

programmeradmin1浏览0评论

I would like to add an event listener to a select element and execute an action when an option is selected. My HTML code is:

<select id="a_background" name="background" class="widget">
 <option value="1">Yes</option>
 <option value="0" selected="selected">No</option>
</select>

So when Yes is selected: do something, If No is selected: do something else

Here is what I have so far:

var activities = document.getElementById("a_background");
var options = activities.querySelectorAll("option");

activities.addEventListener("changed", function() {

  if (options == "addNew")
  {
    alert('add New selected');
  }

 else 
  { 
    alert('add None selected');
  }
});

Due to restrictions I cannot call a function within the select element. For example <select onChange="myFunction()" id="a_background"> That is why I would like to add an event listener.

I would like to add an event listener to a select element and execute an action when an option is selected. My HTML code is:

<select id="a_background" name="background" class="widget">
 <option value="1">Yes</option>
 <option value="0" selected="selected">No</option>
</select>

So when Yes is selected: do something, If No is selected: do something else

Here is what I have so far:

var activities = document.getElementById("a_background");
var options = activities.querySelectorAll("option");

activities.addEventListener("changed", function() {

  if (options == "addNew")
  {
    alert('add New selected');
  }

 else 
  { 
    alert('add None selected');
  }
});

Due to restrictions I cannot call a function within the select element. For example <select onChange="myFunction()" id="a_background"> That is why I would like to add an event listener.

Share Improve this question edited Nov 1, 2017 at 15:53 Mariton asked Nov 1, 2017 at 15:15 MaritonMariton 6213 gold badges14 silver badges29 bronze badges 4
  • Have you done any research into "javascript event handlers" or jquery event handlers? What specifically have you tried and what issues are you running into? – scrappedcola Commented Nov 1, 2017 at 15:23
  • Please check my answer. – m00n Commented Nov 1, 2017 at 15:27
  • @scrappedcola I've done research on both. I would like to use pure JavaScript since I'm not able to add a Jquery library – Mariton Commented Nov 1, 2017 at 15:29
  • @scrappedcola I updated my question to include a code example. – Mariton Commented Nov 1, 2017 at 15:53
Add a comment  | 

4 Answers 4

Reset to default 10

That could be simply done using the addEventListener() function :

document.querySelector('#a_background').addEventListener("change", function() {
  if (this.value == "1") {
    console.log('Yes selected');
  }else{
     console.log('No selected');
  }
});
<select id="a_background" name="background" class="widget">
 <option value="1">Yes</option>
 <option value="0" selected="selected">No</option>
</select>

You can use jquery .change() function for this. Inside this function you can compare your value and do your operations accordingly.

Read Here

$(document).ready(function() {
  $("#a_background").change(function() {
    if ($(this).val() == "1") {
      console.log("Yes");
    } else {
      console.log("No");
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<select id="a_background" name="background" class="widget">
 <option value="1">Yes</option>
 <option value="0" selected="selected">No</option>
</select>

document.getElementById("a_background").addEventListener("change", function(){
     var e = document.getElementById("a_background");
     var selected = e.options[e.selectedIndex].text;
     if(selected =='yes')
        doSomething();
});

Try use onchange="myFunction()" instead of onChange="myFunction()"

function myFunction()
{
  console.log(document.getElementById("a_background").value == 1 ? 'Yes selected' : 'No selected');
}
<select id="a_background" name="background" class="widget" onchange="myFunction()">
 <option value="1">Yes</option>
 <option value="0" selected="selected">No</option>
</select>

发布评论

评论列表(0)

  1. 暂无评论