JavaScript Keep the values of each input in an js object

I need to keep the values of each input in an js object. This is my html code :

<div class="produs_varianta">
      <input type="text" class="product_name" value="Product One">
      <input type="text" class="product_code" value="Code One">
</div>
 <div class="produs_varianta">
      <input type="text" class="product_name" value="Product Two">
      <input type="text" class="product_code" value="Code Two">
</div>
 <div class="produs_varianta">
      <input type="text" class="product_name" value="Product Three">
      <input type="text" class="product_code" value="Code Three">
</div> 

My .js code :

var variante = $('.produs_varianta');
var produs_varianta = [];
variante.each(function(index){
     produs_varianta.push(
     {
         'produs' : 'the value of product_name',
         'cod'    : 'the value of product_code
     }
   );
});

** I tried using $(this).children('.product_name').val() but i've got undefined ! **

Answer:1

You need to add a jquery function for this. Your HTML is

<div class="produs_varianta">
      <input type="text" class="product_name" value="Product One">
      <input type="text" class="product_code" value="Code One">
</div>
 <div class="produs_varianta">
      <input type="text" class="product_name" value="Product Two">
      <input type="text" class="product_code" value="Code Two">
</div>
 <div class="produs_varianta">
      <input type="text" class="product_name" value="Product Three">
      <input type="text" class="product_code" value="Code Three">
</div>

And the JQuery will be this

var result = [];
$('.produs_varianta').each(function( ) {
  var elem = $(this);
  var obj = {
     'product_name': elem.find('.product_name').val(),
     'product_code' : elem.find('.product_code').val()
  };
  result.push(obj);     
});

console.log(result);

Here is the attached link of JSFIDDLE

Answer:2

I've looked through other posts, and can't quite find the answer I'm looking for. I'm a REALLY new PHP programmer... I currently have the following code: $error_message .= 'The Email Address you ...

I've looked through other posts, and can't quite find the answer I'm looking for. I'm a REALLY new PHP programmer... I currently have the following code: $error_message .= 'The Email Address you ...

I'm currently having a problem where when I try to insert data I'm receiving an error that the table column does not exist but I have defined it. I've currently isolated the problem and decided to ...

I'm currently having a problem where when I try to insert data I'm receiving an error that the table column does not exist but I have defined it. I've currently isolated the problem and decided to ...

  1. javascript database transaction
  2. javascript database transaction example
  3. javascript db.transaction
  4. javascript db transaction callback
  5. javascript db.transaction not working
  6. node js database transaction
  7. js db.transaction

I'm trying to get forever-monitor working in node-webkit. I've used it in node express applications in the past without any problems, but can't seem to make it work in this case. I have an index....

I'm trying to get forever-monitor working in node-webkit. I've used it in node express applications in the past without any problems, but can't seem to make it work in this case. I have an index....

  1. using forever-monitor

I am currently working on importing png or svg files onto a canvas using the fabric.js library and an input file button. The code below works only if the image is in the root folder. I know that I don'...

I am currently working on importing png or svg files onto a canvas using the fabric.js library and an input file button. The code below works only if the image is in the root folder. I know that I don'...