How to get value from div and put it in img tag src in php

Value

is derived from this table when a row is pressed:

<script>
    var link_base = "<?php echo bloginfo('template_url').'/savelabel/'; ?>" ;
    var img_name = $('#imgdiv').text();
    $('#imgid').attr('src', link_base + img_name);
</script>
<div class="bx2">
    <img id="imgid" alt="" class="imgsty" />    
</div>

The selected file name is displayed in the div:

<div id="txtdiv"></div>

And inside html there is an image tag in which I want to pass the value div(i.e. the name of the image file) src=""in the img tag.

The code I'm trying to achieve is shown below:

<script>
  var link_base = "<?php echo bloginfo('template_url').'/savelabel/'; ?>" ;
    var img_name = $('#imgdiv').text();
    $('#imgid').attr('src', link_base + img_name);
</script>
<div class="bx2">
    <img id="imgid" alt="" class="imgsty" />    
</div>
+4
source share
3 answers

wrap the script in $(document).ready(function(){...your code ...});, this ensures that your DOM structure is ready for processing, since the image in your case will be ready to modify src

<script>
  $(document).ready(function(){
  var link_base = "<?php echo bloginfo('template_url').'/savelabel/'; ?>" ;
    var img_name = $('#imgdiv').text();
    $('#imgid').attr('src', link_base + img_name);
 });
</script>
+6
source

Move the script under img- when you try to set the attribute, the srcimage is missing.

<div id="txtdiv"></div>

<div class="bx2">
    <img id="imgid" alt="" class="imgsty" />    
</div>
<!-- here or in header link jQuery -->
<script>
    var link_base = "<?php echo bloginfo('template_url').'/savelabel/'; ?>" ;
    var img_name = $('#txtdiv').text();
    $('#imgid').attr('src', link_base + img_name);
</script>
+1

. onload https://jsfiddle.net/2z7ux3et/2/

, , :

<div id="imgdiv">Information_icon.svg</div>
<div class="bx2">
    <img id="imgid" alt="" class="imgsty" />    
</div>
$(function() {
    var link_base = 'http://upload.wikimedia.org/wikipedia/commons/3/35/' ;
    var img_name = $('#imgdiv').html();
    $('#imgid').attr('src', link_base + img_name);
});
0
source

All Articles