0

I have html listbox as, which has same name but different id

<select name="dept"  id="vrow" >
                        <option selected="selected" > - Select Department- </option>
                        <option value="GEN">computer </option>
                        <option value="SC">mac </option>
                        <option value="ST">civ </option>
                        <option value="OBC">ele </option>
                      </select>

i had atteched it to each row fetched from database, how can i get ID if i change perticular listbox value.

1
  • you use ajax function for get results from server database Commented Jan 30, 2016 at 12:30

3 Answers 3

1

If you need id then use:

document.getElementById('vrow').addEventListener('change', function(e) {
   if (e.target.name==='dept') {
     alert(e.target.id);
   }
})

Working Demo

If you need value then use:

document.getElementById('vrow').addEventListener('change', function(e){
   if (e.target.name==='dept') {
       alert(e.target.value);
   }
})

Working Demo

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

Comments

0

You Write Javascript Function or Jquery function

like this

<select onchange="dept(this.value)" name="dept"  id="vrow" >
<option selected="selected" > - Select Department- </option>
<option value="GEN">computer </option>
<option value="SC">mac </option>
<option value="ST">civ </option>
<option value="OBC">ele </option>
</select>

<script type="text/javascript">
function dept(id){
$.ajax({
url: 'ajax.php',
data: { id: id} ,
success: function (response) {
try{
alert(response);
}
catch(e){
alert("Error"+e);
}
},
error: function (e) {
alert("error"+e); or alert("error"+JSON.stringify(e));
}
});
}
</script>

Ajax.php

Comments

0

You can add event listener on change event to parent node of all <select> elements it can ul if they are in list or even body.

<script type="text/javascript">
  document.getElementByTagName('UL').addEventListener('change', function(e) {
    if (e.target.name==='dept') {
      //In this case you have got element that you need and its id 
      //e.target.id
      alert(e.target.id);
    }
  });
</script>

2 Comments

Will you please tell me what change i have to make in html listbox coding?
Insert that code before you close 'body' tag (</body>).

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.