Search code examples
jquerychaining

jquery proper chaining?


I have the following:

$("#modal-controls").html(html)
  .find("#pic_caption").val(data.pic_caption)
  .find("#pic_desc").val(data.pic_desc);

Basically, I change the html of something, search for inputs and change their values. it changes the 1st one, but doesn't proceed to change the 2nd input, How will I go about writing it properly using chaining? or must I seperate it to 2 different jQuery objects?

Thanks.


Solution

  • Have a look at jQuery.fn.end(). You can add it to the chain to revert back to the previous set of matched elements.

    $("#modal-controls").html(html)
      .find("#pic_caption").val(data.pic_caption).end() //<--
      .find("#pic_desc").val(data.pic_desc);