0

I am trying to get the inputs from all of the input boxes. Then add to an array and printing this via a <p> class.

The trigger should be a button that then starts a for loop across all the input classes. Am I missing something simple?

HTML

<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>

<div id="p_scents">
    <p>
        <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
    </p>

</div>
<p class="result"></p>
<button id="add">SUMBIT

JQUERY

$(function() {
        var scntDiv = $('#p_scents');
        var i = $('#p_scents p').size() + 1;

        $('#addScnt').live('click', function() {
                $('<p><label for="p_scnts"><input type="text" class="encdom_local" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
                i++;
                return false;
        });

        $('#remScnt').live('click', function() { 
                if( i > 2 ) {
                        $(this).parents('p').remove();
                        i--;
                }
                return false;
        });
});
$("button#add").click(function() {
    var encdom_local = [];
    $('.encdom_local').each(function() {
        values.push(encdom_local.val()+',');
    });
    $(".result").html(encdom_local.join(""));
});

http://jsfiddle.net/tZPg4/16740/

2
  • 1
    values.push( I dont see the variable values declared anywhere Commented Oct 27, 2016 at 13:39
  • @NikhilNanjappa is correct. If you look at the JS console on your fiddle. It throws an error that values is not defined. Commented Oct 27, 2016 at 13:44

1 Answer 1

2

You should get value of input (not array) and then push to the array you created above. Then use join with coma separator instead of pushing val() + ","

$("button#add").click(function() {
    var encdom_local = [];
    $('.encdom_local').each(function() {
        encdom_local.push($(this).val());
    });
    $(".result").html(encdom_local.join(","));
});
Sign up to request clarification or add additional context in comments.

Comments

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.