0

Below is my HTML

I want a script like this in one Textbox I want to add all the amount total and display it and in one more textbox I want it like this

"name": [{
  "id": 3,
  "qty": 2
}, {
  "id": 4,
  "qty": 5
}]

My Updated Question

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
  <input type="text" name="name">
  <input type="text" name="id">
  <input type="text" name="qty">
  <input type="text" name="amount">
</div>
<div class="main">
  <input type="text" name="name">
  <input type="text" name="id">
  <input type="text" name="qty">
  <input type="text" name="amount">
</div>
<div class="main">
  <input type="text" name="name">
  <input type="text" name="id">
  <input type="text" name="qty">
  <input type="text" name="amount">
</div>
<button>Click Me!!!</button>

My Updated code not working in this js core js is working fine

3
  • What do you mean by "I want a script like this in one Textbox"? Commented Dec 5, 2017 at 4:50
  • onchange qty textbox i want to add like i gave Commented Dec 5, 2017 at 4:51
  • How do you calculate qty and id in the output? Commented Dec 5, 2017 at 5:00

2 Answers 2

1

Try the following way:

$('input[name="qty"]').change(function(){
  var name = [];
  $('.main').each(function(index,item){
    var obj={};
    obj.id = $(item).find('input[name="id"]').val();
    obj.qty = $(item).find('input[name="qty"]').val();
    name.push(obj)
  });
  console.log(name);
});
input{
  width:100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
    <input type="text" name="name">
    <input type="text" name="id">
    <input type="text" name="qty">
    <input type="text" name="amount">
</div>
<div class="main">
    <input type="text" name="name">
    <input type="text" name="id">
    <input type="text" name="qty">
    <input type="text" name="amount">
</div>
<div class="main">
    <input type="text" name="name">
    <input type="text" name="id">
    <input type="text" name="qty">
    <input type="text" name="amount">
</div>

Sign up to request clarification or add additional context in comments.

Comments

0
var s = { 'name' : [] };
var total =  0; 
$(".main").each(function(){
     s['name'].push({ 'id': $(this).find("input[name='id'"]).val(),'qty': $(this).find("input[name='qty'"]).val()});

     total = total + parseFloat($(this).find("input[name='total'"]).val());

});

//total gives final sum //object s gives the array of objects.

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.