Search code examples
jqueryformshtml-tablevarprepend

JQuery: Prepending a form to a <tr>


I have a html table and I want to append a form to each tr. I have written this:

<script>

var bform = '<form name="input" action="html_form_action.asp" method="get"><input type="hidden" value="event"></input><input type="submit" value="Submit" /></from>';

$('tr').prepend(bform);

</script>

But it does not seem to be working for me, nor is it throwing an error in the console.

Could some one point me in the right direction?


Solution

  • Add tr and td to your bform

    var bform = '<tr><td> YOUR FORM HERE</td></tr>'
    

    When you prepend a string to a tr, you are inserting inside a table, so, your current code is invalid:

    <table>
        <form name="input" action="html_form_action.asp" method="get">
            <input type="hidden" value="event"></input>
            <input type="submit" value="Submit" />
        </from>';
        ^^^^^ INVALID HTML ^^^^^
    
        <tr>
            <td>CONTENT</td>
        </tr>
    </table>
    

    As Blender said, make sure you put the code inside a jQuery(document).ready(function($){})

    Additionally, you don't need to close inputs, so, both are valid codes:

    <input type="hidden" value="event">
    <input type="submit" value="Submit">