Stranger
Stranger

Reputation: 125

how to get input value in blade

I'm trying to get the value of the previous input in Blade.

<input name="people" type="number" cols="5">

    @for($i=1; $i<$people; $i++)
        <input type="text" cols="50">
    @endfor

Depending on the number value from first input I want to generate input fields in the same Blade document. But the shown trick is wrong, I'm getting an error "Undefined variable: people". Is it possible to do that? I am using Laravel 5.2.

Upvotes: 0

Views: 4493

Answers (1)

Javid Aliyev
Javid Aliyev

Reputation: 436

It doesn't depend on blade. You can use the code below in anywhere.

    <head>
        <title>Test</title>
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
    </head>
    <body>
        <div style="margin: 20px 10px;">
            <input name="people" id="people" type="number" value="2" cols="5">
            <script type="text/javascript">
            $('#people').change(function(){

                $('#inputs').html('');

                for (var i = 0; i < $('#people').val(); i++) {
                    $('#inputs').append('<input type="text" name="input_'+$('#people').val()+'" value="'+$('#people').val()+'"><br>');
                }
            });
            </script>
        </div>

        <div id="inputs"></div>
    </body>
</html>

Upvotes: 1

Related Questions