0

I have an issue when i'm trying to add rows in a table using Javascript and jQuery.

My code:

<script>
$(document).ready(function(){
   for (i=0; i<myvar.length; i++){
      $("#items").after('<tr class="item-row"><td class="item-name"></td></tr>');
    }
});
</script>

My problem is that the new row did not take the style of my CSS file. CSS file loaded in <head>.

Did i miss something?

4
  • 1
    Make a fiddle please Commented Apr 26, 2016 at 10:45
  • 3
    what is items element? Commented Apr 26, 2016 at 10:46
  • 2
    if #items is the table, then you should use $("#items").append, not .after to add the elements to the table, not next to it. Commented Apr 26, 2016 at 10:47
  • @pawel the append is the solution to my problem. Thanks! Commented Apr 26, 2016 at 10:57

1 Answer 1

1

Use .append instead of .after because .after is adding tr outside of the table that's why the CSS is not applying:

for (i=0; i<5; i++){
      $("#items").append('<tr class="item-row"><td class="item-name"></td></tr>');
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Wowsk and Dhara_Parmar this is the correct answer. I'm feeling fool right now because i did not check it before. Thanks!

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.