3

I have a form in php with dynamically added rows (after clicking button the row is added). I want to fill the field with value "xx" and i want to do it in jquery.

This loop create the dynamically added rows in jquery. I want to fill added fields with value "xx":

while($personsArrayLength>2){
        echo '
            <script>
            $(document).ready(function(){
                var i = 2;
                var rowTemplate2 = jQuery.format($("#template2").html());

                rowTemplate2.value = "xx";
                addRow2();

                function addRow2(){
                    var ii = i++;
                    $("#app_here2").append(rowTemplate2(ii));
                    $("#delete_" + ii).click(function(){
                        $("#row_" + ii).remove();
                    });
                }
            });
        </script>
    ';

Here is html for that:

function addRows2(){

    global $personsError_css;
    $personsArray = $_POST['persons'];
    JB_var_dump($_POST['whichRow']);
        $html = '<table id="template2" align="right" style="display:none; ">
            <tr id="row_{0}">
                <td><input type="text" size="52" id="persons" name="persons[]" maxlength="100"></td>
                <td><img src="/../_img/row_del.png" id="delete_{0}" alt="usun"></td>
            </tr>
        </table>
        <table id="list2" style="margin-left:200px;">
            <thead >
                <tr>
                    <th></th>
                    <th></th>
                </tr>
            </thead>

            <tbody>
                <tr>
                    <td><input type="text" size="52" name="persons[]" maxlength="100" style="'.$personsError_css.';" value="'.$personsArray[1].'"></td>
                    <td></td>
                </tr>

                <tr>
                    <td colspan="2" id="app_here2"></td>
                </tr>
            </tbody>
        </table>';
        return $html;
} 

This is properly filled form
enter image description here

In this epty fields I want to add values "xx" enter image description here

Sorry for my english.

How can i set values in added rows? What i should change in my code? Thanks for help.

1
  • do you want to set a placeholder? until the person types in something the placeholder will fill the textbox else.. rowTemplate2.find('input:empty').val('xx') Commented Nov 6, 2014 at 12:08

1 Answer 1

1

Change your 'addRow2' to this:

function addRow2(){
    var ii = i++;
    $("#app_here2").append(rowTemplate2(ii));
    //UPDATE: var rowTemplate2 is not a jQuery Object, as i thought
    $("#template2").find("input:empty").val("xx");; // added this row
    $("#delete_" + ii).click(function(){
        $("#row_" + ii).remove();
    });
}
Sign up to request clarification or add additional context in comments.

8 Comments

It doesn't work unfortunettly. The script is stopped in moment: 'rowTemplate2.find('input:empty').val('xx')' and doesn't continued.
add ';' .. so copy my solution again, because i missed a semicolon.. but i dont think, that this will cause the problem... may you press F12 and say me, what you see?
Found my error. var rowTemplate2 is not a jQuery Object, as i thought
It's not semicolon. Here is link after my F12 clicking: link
ok... i expected the console :D .. do you know about developer tools? which browser ure using? is your code in your post working? i dont know jQuery.format()
|

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.