Search code examples
javascriptjqueryhtmlinsertcontain

Insert html based on contain



In this situation, I have one page, which will load with all different kinds of content (example: 5 different clothing catalogs).

I'm looking for a way to insert html based on specific text in a header. I know this isn't right, but this may give you an idea of what I'm trying to do.

$jq(document).ready(function(){
  $jq("h3:contains('Men')").insertBefore("<img src='images/men.png'>");
});

The end result I'm going for is being able to load different images on the same page depending on the specific text of the header.

Thank you!!!


Solution

  • You can append the img based on the contains criteria:

    $("h3:contains('Men')").append("<img src='images/men.png'>");
    

    JS Fiddle