0

--UPDATE--

here is my jsfiddle:

https://jsfiddle.net/18g8np7b/

I have a jquery datatable (www.datatables.net) where one of the columns has a dropdown menu. I assigned a change listener to my select dropdown. My problem is the listener only activates if I the select dropdown on the first row is selected. My table currently has 20 rows, but the change listener/event only activates if the select on the top row is changed. Here's my table:

<table id="projects_table" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
          <thead>
            <tr>
              <th>Project Code</th>
              <th>Project Name</th>
              <th>Project Manager</th>
              <th>Client</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
            {{#each projects}}
            <tr>
              <td>{{projectcode}}</td>
              <td>{{projectname}}</td>
              <td>{{projectmanager}}</td>
              <td>{{client}}</td>
              <td>
                <select class="form-control" id="actions">
                  <option selected>Select action</option>
                  <option>View Details</option>
                  <option>Export to CSV</option>
                </select>
              </td>
            </tr>
            {{/each}}
          </tbody>
        </table>

Here's my .js file:

$("#actions").change(function() {
    alert('change!!'); ////only works if there's a change in the select dropdown on the first row
    console.log($(this).find("option:selected").text());
});

Can someone help?

Thanks in advance!

3
  • Unless I'm missing something, your code seems to work? Can you clarify what you expect to happen? jsfiddle.net/ufqhf3ew Commented Feb 4, 2016 at 17:43
  • Hi - I expect it to alert('change!!') everytime I click a different item on the dropdown menu. Right now it only works if i click a different item on the dropdown menu on the first row; if I click a different item on any other row, it doesn't work.. Commented Feb 4, 2016 at 17:49
  • i updated jsfiddle to make it easier to understand - jsfiddle.net/18g8np7b Commented Feb 4, 2016 at 17:53

2 Answers 2

2

try bind the event using a class , the id bind only works in one element because in the DOM shouldn't exist more than one element with the same id.

html:

<select class="form-control">
              <option selected>Select action</option>
              <option>View Details</option>
              <option>Export to CSV</option>
</select>

script:

$('.form-control').change(function() {
   console.log('change!!');
   console.log($(this).find("option:selected").text());
});
Sign up to request clarification or add additional context in comments.

Comments

1

The issue is that you're binding to an id - the intention of this attribute is that they should be unique in the DOM.

What you want to do is create a class and bind to that instead.

https://jsfiddle.net/18g8np7b/5/

HTML:

<table id="projects_table" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Project Code</th>
      <th>Project Name</th>
      <th>Project Manager</th>
      <th>Client</th>
      <th>Actions</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>abc</td>
      <td>123</td>
      <td>abcd</td>
      <td>123d</td>
      <td>
        <select class="form-control" id="actions0">
          <option>Select action</option>
          <option>View Details</option>
          <option>Export to CSV</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>abc</td>
      <td>123</td>
      <td>abc</td>
      <td>123</td>
      <td>
        <select class="form-control" id="actions1">
          <option>Select action</option>
          <option>View Details</option>
          <option>Export to CSV</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>abc</td>
      <td>123</td>
      <td>abc</td>
      <td>123</td>
      <td>
        <select class="form-control" id="actions2">
          <option>Select action</option>
          <option>View Details</option>
          <option>Export to CSV</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>abc</td>
      <td>123</td>
      <td>abc</td>
      <td>123</td>
      <td>
        <select class="form-control" id="actions3">
          <option>Select action</option>
          <option>View Details</option>
          <option>Export to CSV</option>
        </select>
      </td>
    </tr>
  </tbody>
</table>

JS:

$('#projects_table').DataTable({
  responsive: true
});


$(".form-control").change(function(e) {
  alert('change!!'); //only works if there's a change in the select dropdown on the first row
  console.log($(this).find("option:selected").text());

  console.log(e.currentTarget)
});

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.