Search code examples
jqueryappendto

AppendTo Not Working On Webpage


I have this jQuery script:

$('.company').each(function(i, item) {
  var tempTitle = item.title;
  $('Working').appendTo("div[title='" + tempTitle + "']");
});

and this is the HTML:

<li><div class="company" title="32"></div><div class="shortdescription">Short Description Here</div></li>
<li><div class="company" title="33"></div><div class="shortdescription">Short Description Here</div></li>
<li><div class="company" title="34"></div><div class="shortdescription">Short Description Here</div></li>
<li><div class="company" title="35"></div><div class="shortdescription">Short Description Here</div></li>
<li><div class="company" title="36"></div><div class="shortdescription">Short Description Here</div></li>

and no details are added in the divs.

Where is the mistake I make? O_o


Solution

  • appendTo requires HTML or jQuery wrpped element to append. You are passing simple string working.

    Try this:

    $('.company').each(function(i, item) {
      var tempTitle = item.title;
      $('<div>appeded by jQuery</div>').appendTo("div[title='" + tempTitle + "']");
    });
    

    Now replace <div>appeded by jQuery</div> with the HTML you want to append.