How to put Html snippet on all child elements except the last one?

Asked

Viewed 147 times

11

I have a dynamically generated table, and I want to put a input within each td. I can get the id of the tr, then I do:

$("#id").children().html("<input type='text' value='" + valor + "' />");

So he puts a input within each td that tr, but I don’t want him to put the input last td, how can I do this?

3 answers

11


Use the operator .not() combined with the selector :last-Child

$("#id").children().not(":last-child").html("<input type='text' value='" + valor + "' />");

6

3

The Children function can receive a selector as parameter, and in this selector you can directly use :not as well as :last.

$("#id").children(":not(:last)").html("<input type='text' value='" + valor + "' />");

Example: http://codepen.io/silviolucenajunior/pen/ZQyNjx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.