Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get value from div and place it within img tag src in php

Tags:

html

jquery

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>
like image 566
Suneet Avatar asked Mar 16 '23 06:03

Suneet


2 Answers

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>
like image 151
Bhushan Kawadkar Avatar answered Apr 01 '23 12:04

Bhushan Kawadkar


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>
like image 38
pavel Avatar answered Apr 01 '23 14:04

pavel