0

I have a basic problem with selecting elements using jQuery. I have dynamically added table rows in a table, each with a dynamically generated ID.

I basically have this code that I was using when the table rows did not have an ID. Now, I have added other features which necessitated assigning table rows with IDs.

This was the code I was using before to toggle certain columns in each row:

$("#row .col[data-col='" + col + "']").toggle();

This is how I am assigning IDs to my rows:

<tr id="row_'+(currentIndex-1)+'">

How can I now use jQuery to select the above row with it's dynamic ID and use it with my code to toggle my columns?

I have tried this but id doesn't work:

$("#row_'+(currentIndex-1)+' .col[data-col='" + col + "']").toggle();
4
  • what error are you getting? as the code you mentioned has some issue related to "". $("#row_" + (currentIndex-1) + " .col[data-col='" + col + "']").toggle(); try this Commented Mar 17, 2016 at 12:35
  • I am getting this error in my console: Uncaught Error: Syntax error, unrecognized expression: #row_'+(currentIndex-1)+' .col[data-col='column1'] Commented Mar 17, 2016 at 12:36
  • 2
    yes there an error in your syntax ... try this $("#row_" + (currentIndex-1) + " .col[data-col='" + col + "']").toggle(); Commented Mar 17, 2016 at 12:36
  • That works!! Thank you so much. Please add that as an answer so I can accept it. Thank you :) Commented Mar 17, 2016 at 12:38

1 Answer 1

1

The problem in your code is you have added extra single quote. remove it and try this.

$("#row_" + (currentIndex-1) + " .col[data-col='" + col + "']").toggle();

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

1 Comment

can you please click accept this answer. if it's working fine for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.