value is coming from this table on clicking a row :
<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>
Selected filename is showing in a div :
<div id="txtdiv"></div>
And within html, there is an image tag, in which I want to pass value of div
(ie the name of image file) into src=""
within img tag.
The code which I am trying to achieve this is as 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>
wrap your script in $(document).ready(function(){...your code ...});
, this will ensure that your DOM structure is ready to process like image in your case is ready to change its 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>
Move script under img
- when you try to set src
attribute, no image exists.
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With