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

11

I have a dynamically generated table, and I want to put input inside every td . I can get the id of tr , so I do:

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

So it puts a input within each td of that tr , but I do not want it to put input in the last td , how can I do that?

    
asked by anonymous 12.01.2016 / 14:47

3 answers

11

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

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

You can use as Peter suggested , you can still use .not(":last") :

jsFiddle: link

I usually add .slice(0, -1) to the selector to remove the last element from this selection:

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

jsFiddle: link

    
12.01.2016 / 15:37
3

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

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

Example: link

    
12.01.2016 / 18:01