1

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:

document.sampleForm.image.value = "images/"+img.attr('src')+"";

Javascript:

$('tr', '#target').each(function () {
    var tr = $(this);
    html += '<center><tr>';
    $('td', tr).each(function () {
        var td = $(this);
        if (!td.is('td.header')) {
            var img = $('img', td);
            var text = $('textarea', td).val();
            html += '<td style="width: 33%;">';
            html += (img.length) ? '<img src="' + img.attr('src') + '" style="display: block; max-width: 100%;" />' : '<div style="width: 100%;"></div>';
            document.sampleForm.image.value = "images/"+img.attr('src')+""; 
            document.forms["sampleForm"].submit();
            html += '</td>';
        } 
    });
    html += '</tr></center>';
});

HTML:

<table id="target">
<form id="sampleForm" name="sampleForm" action="test.php" method="post">
    <tr><center>

        <td class="logo" colspan="3">
            <h3>Choose Header</h3>
          <div class="img-container"><input type="hidden" name="image" id="image" value=""></div>

        </td>

    </center>
    </tr>
    <tr>
    <td style="height:auto;">

    <?php

    $test = mysql_query("SELECT id, title FROM tmpl2");



    echo "<select class='image_select' id='logo_option' name='logo_option[]' multiple='multiple'>";


        while ($row = mysql_fetch_assoc($test))
    {
        echo "<option value='".$row[id]."'>$row[title]</option>";
    }   


    echo "</select>";



    ?>



    </td>
    </tr>


           <tr><center>

        <td class="logo" colspan="3">
                <h3>Choose Logo</h3>
          <div class="img-container"></div>

        </td>

    </center>
    </tr>
    <tr>
    <td style="height:auto;">
    <?php

    $test = mysql_query("SELECT id, title FROM tmpl2");

    echo "<select class='image_select' id='header_option' name='header_option[]' multiple='multiple'>";


        while ($row = mysql_fetch_assoc($test))
    {
        echo "<option value='".$row[id]."'>$row[title]</option>";
    }   


    echo "</select>";

    ?>
    </td>
    </tr>

</table>
3
  • 3
    Doing a submit inside a loop doesn't make sense. When you submit the form, the page reloads, and your script stops. Commented Nov 4, 2013 at 22:42
  • Use jquery function attr: $("your_input").val("images/"+img.attr('src')); for more details add your code Commented Nov 4, 2013 at 22:45
  • Barmar - Yes, that was a copy error. I have updated the code. Sabri - I have updated my post to show my HTML. Commented Nov 4, 2013 at 22:50

1 Answer 1

1

The reason you are getting undefined is because

document.sampleForm.image.value = "images/"+img.attr('src')+"";

is not a property of the HTMLImageElement. You need .src .

document.sampleForm.image.src = "images/"+img.attr('src')+"";

but as @Barmar said you really should not be doing it like this.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there any other way you suggest to save the image URL to a post variable? Using img.src does not work, it outputs a blank for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.