0

I needed to get the values of each dynamically created rows and print those values. What the current code does is that it can alert the all the added values or print to console. Now, how can I print those values?

For instance user has added one more row and selected the following:

enter image description here

After the user clicks Get Values, all the values (Female, Lady, Male and Schertz) must be printed to a generated table and page should look like this:

enter image description here

Now what I have are the following code.

HTML view:

<button>Add More Order</button>
<button>Delete Added Order</button>

<div id="wrap" class="order-container">
  <p>       
    <select class="getThis">
      <option>--Gender--</option>
      <option value="Male">Male</option>
      <option value="Female">Female</option>
    </select>   
    <input type="text" class="getThis" placeholder="Name"/>
  <br>
  </p>
</div>

  <input type="submit" id="getValues" value="Get Values"></input>

jQuery:

$(document).ready(function(){

    //Add new row   
    $("button:nth-of-type(1)").click(function(){
        $("#wrap").append("<p><select class='getThis'><option>--Gender--</option><option>Male</option><option>Female</option></select><input type='text' class='getThis' placeholder='Name'/><br></p>").trigger('create');
    });

    //Remove last added row
    $("button:nth-of-type(2)").click(function(){
        $("p:last-of-type").remove();
    });

//alert values
$("#getValues").click(function() {
        $(".getThis").each(function() {
           var getThis =  $(this).val();
           //console.log(getThis);
           alert(getThis);

     });

  });

}); // Document Ready End

Here's a JSfiddle for a demo.

Any ideas? Kamsahamnida!

5
  • How can I put the values into input type=text Which input??? Commented Apr 28, 2014 at 8:33
  • can you plz explain in which textbox you want value. Commented Apr 28, 2014 at 8:38
  • My apologies, @A.Wolff, I was not able to clear which input I'm referring to. I edited my question. Anyway, I'm planning to get all the values the user has entered (the name and gender) and put those values to an <input type="text">. Commented Apr 28, 2014 at 8:42
  • Please, consider to explain expected behaviour providing relevant sample. You want ONLY dynamic elements? What if more than one dynamic element, must create more input text or fill already existing ones? So then, should e.g names separated by comma or what? Etc... Still unclear what you are asking Commented Apr 28, 2014 at 8:49
  • @A.Wolff, I updated my post. Was I able to clear my question? Thanks. :) Commented Apr 28, 2014 at 9:07

4 Answers 4

1

You could use e.g:

DEMO

$("#getValues").click(function () {
        var $table = $('#resultTable').length ? $('#resultTable').find('tr:gt(0)').remove().end() : $('<table/>').append('<tr><th>Gender</th><th>Name</th>').appendTo('body').attr('id', 'resultTable');
        var getValues = $("p:has(select.getThis)").find('select.getThis').map(function () {
            return {
                gender: this.value,
                name: $(this).next().val()
            }
        }).get();
        $.each(getValues, function (_, fields) {
            $table.append($('<tr/>', {
                html: '<td>' + fields.gender + '</td><td>' + fields.name + '</td>'
            }))
        });
    });
Sign up to request clarification or add additional context in comments.

Comments

1

try this:

 $("#getValues").click(function() {
    $("select.getThis").each(function() {
       var getThis =  $(this).val();
       $(this).next().val(getThis);
 });});

Working Demo

2 Comments

+1 I understand question like this too even doesn't make much sense
Thanks for answering, @Milind Anantwar. Something like that. But I'll try to work on your code. Btw, I updated my post. :)
1

try

document.getElementById('getValues').value;

Comments

1

Change your inputs class to something else as it might start confilcting and then try this:

$("#getValues").click(function() {
  $(".getThis").each(function() {
    var getThis =  $(this).val();
    $(this).closest("p").find("input[type='text']").val(getThis);
  });
});

What it does is it goes from the current target to the closest parent paragraph tag and from there it finds the input[type='text'] and gives it the value

1 Comment

Hi, @Karl Viiburg. I appreciate your answer. I have changed my question above. :)

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.