سؤال

<div class="parent">   
 <select id="lft_sel_power" onchange="alter()" class="select">
    <option value="lsp" selected="selected">Select Power</option> 
    <option value="-3">-3</option>
    <option value="-2">-2</option> 
    <option value="-1">-1</option>
    <option value="+1">+1</option> 
    <option value="+2">+2</option> 
    <option value="+3">+3</option> 
    </select>
</div

In this drop down i need to get alert for minus values & need to add a class for parent div how can i do this with using javascript Plz help me

هل كانت مفيدة؟

المحلول

You need to handle onchange event like this,

document.getElementById('lft_sel_power').onchange = function () {
    this.parentNode.className = "";
    var selected = this.options[this.selectedIndex].value;

    if (selected < 0) {
        this.parentNode.className = "className";
        alert(selected);
    } else {
        // code here
    }

};

Working fiddle

نصائح أخرى

Try this instead.

Jquery:

$('#lft_sel_power').change(function(){
  if(parseInt($(this).val()) < 0){
      alert($(this).val());
      $(this).parent().addClass('someClass');
  }
});

JSFiddle

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top