-1

I have a table and inside this table every row is generated using ajax. In every row there is a select tag with an id(selectTax). I want to make a js function that occur when this select selected. For this i am using

$("#selectTax").change(function(){
  console.log("vikash");
});

But when i am clicking on this nothing happening. Help me soon please.

VikashSDNT
  • 41
  • 1
  • 6
  • 1
    Identifiers in HTML must be __unique__, instead assign a common class then use Class selector and for dynamically generated elements use [Event Delegation](http://learn.jquery.com/events/event-delegation/) – Satpal Oct 31 '17 at 06:54
  • Sir I also Used class in that Select but nothing happening. – VikashSDNT Oct 31 '17 at 06:59
  • `$(document).on("change", ".selectTax",function(){ console.log("vikash", $(this).val()); });` – Satpal Oct 31 '17 at 07:00
  • You canoot assign the same id to more than 1 element, also try to use 'this' instead of using the id – Ram Segev Oct 31 '17 at 07:01
  • 1
    Hi Satpal thanks alot bro. Now it is Working. – VikashSDNT Oct 31 '17 at 07:01

2 Answers2

0

There are two issue with your code:

. Identifiers in HTML must be unique. So use class selector instead.

. For dynamic html, use $(document).on() like:

$(document).on('click', '.selector', function(){
  // your logic here
});
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0

try this i have not added ajax code but similar to your code i have generated dynamic select tag and added its change event.

check your console for errors if any

function AddSelect(){
    var select = $("<select class='content-select'><option>1</option><option>2</option></select>");
    $("#content").append(select);
}

$(document).on('change', '.content-select', function(){
    alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="Add Select tag" onclick="AddSelect()">

<div id="content">
</div>
Dhaval Pankhaniya
  • 1,996
  • 1
  • 15
  • 26