Search code examples
jqueryconditional-comments

Using jQuery .attr to replace image inside conditional comment, not working


I'm trying to show a different image to site visitors using IE 8 or older, perhaps I'm missing something, but it seems this should be working, any help is appreciated.

<!--[if lte IE 8]>
  <script>
    $("#myImage").attr("src", "/wp-content/themes/TRTheme/images/Chester-4317.jpg");
  </script>
<![endif]-->

<div id="mapOhio"> <img id="myImage" src="<?php bloginfo('template_url'); ?>/images/ohioMap.png" width="253" height="285" alt="Image of a map of Ohio." usemap="#image_map" border="0"/> </div>

Solution

  • You are trying to access the element before it exists. Reverse the order. Answer in comments, thanks to Felix King.