Javascript question: change SRC value of IMG

Pro
  • 114 points
    Serious WPMU DEV-ster
    I'm helpful
    Giovanni

    Member  —  4th February 2012 (3 months ago)         

    I am trying to change an IMG tag's SRC value when a button gets clicked. The button is on an iFrame and the IMG tag is on the TOP Document.

    Thank you in advance!

    My button HTML:
    <a href="javascript:void(0)" class="add_featured_btn button-secondary" rel="<?php echo $attach_id;?>">Use as gift image</a>

    My IMG HTML:
    <img id="hm_uploaded_giftimage" src="" alt="Your Gift" />

    I've tried this javascript:::

    <script type="text/javascript">
    $(".add_featured_btn").click(function(){
    $("img#hm_uploaded_giftimage",top.document).attr({ src: 'http://www.ourhoneymoongiftregistry.com/wp-content/uploads/2012/02/IMG_75102-150x150.jpg'});
    });
    </script>

    I've also tried this javascript::::

    echo "<script type=\"text/javascript\">
    		jQuery(\".add_featured_btn\").click(function(){
    		jQuery(\"hm_uploaded_giftimage\",top.document).setAttribute('src', '". $img_preview . ";');
    		});
    		</script>";