Note that there are some explanatory texts on larger screens.

plurals
  1. POSave Img Src to Javascript Variable
    primarykey
    data
    text
    <p>I am trying to set the image src inside a hidden variable that is passed from my form. When I output the variable is "undefined". I am pretty new to Javascript and not sure what the problem could be. My error is on line:</p> <p><code>document.sampleForm.image.value = "images/"+img.attr('src')+"";</code></p> <p>Javascript:</p> <pre><code>$('tr', '#target').each(function () { var tr = $(this); html += '&lt;center&gt;&lt;tr&gt;'; $('td', tr).each(function () { var td = $(this); if (!td.is('td.header')) { var img = $('img', td); var text = $('textarea', td).val(); html += '&lt;td style="width: 33%;"&gt;'; html += (img.length) ? '&lt;img src="' + img.attr('src') + '" style="display: block; max-width: 100%;" /&gt;' : '&lt;div style="width: 100%;"&gt;&lt;/div&gt;'; document.sampleForm.image.value = "images/"+img.attr('src')+""; document.forms["sampleForm"].submit(); html += '&lt;/td&gt;'; } }); html += '&lt;/tr&gt;&lt;/center&gt;'; }); </code></pre> <p>HTML:</p> <pre><code>&lt;table id="target"&gt; &lt;form id="sampleForm" name="sampleForm" action="test.php" method="post"&gt; &lt;tr&gt;&lt;center&gt; &lt;td class="logo" colspan="3"&gt; &lt;h3&gt;Choose Header&lt;/h3&gt; &lt;div class="img-container"&gt;&lt;input type="hidden" name="image" id="image" value=""&gt;&lt;/div&gt; &lt;/td&gt; &lt;/center&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="height:auto;"&gt; &lt;?php $test = mysql_query("SELECT id, title FROM tmpl2"); echo "&lt;select class='image_select' id='logo_option' name='logo_option[]' multiple='multiple'&gt;"; while ($row = mysql_fetch_assoc($test)) { echo "&lt;option value='".$row[id]."'&gt;$row[title]&lt;/option&gt;"; } echo "&lt;/select&gt;"; ?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;center&gt; &lt;td class="logo" colspan="3"&gt; &lt;h3&gt;Choose Logo&lt;/h3&gt; &lt;div class="img-container"&gt;&lt;/div&gt; &lt;/td&gt; &lt;/center&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="height:auto;"&gt; &lt;?php $test = mysql_query("SELECT id, title FROM tmpl2"); echo "&lt;select class='image_select' id='header_option' name='header_option[]' multiple='multiple'&gt;"; while ($row = mysql_fetch_assoc($test)) { echo "&lt;option value='".$row[id]."'&gt;$row[title]&lt;/option&gt;"; } echo "&lt;/select&gt;"; ?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload