0

Currently I have this code for getting multiple weight

 <input type="number" name="myWeight" id="1A">
 <input type="number" name="myWeight" id="1B">
 <input type="number" name="myWeight" id="1C">

This is my current javascript lines

var selWeight = []; 
$('input[name="myWeight"]').each(function() {
    selWeight.push($(this).val());
});
alert(selWeight);

This current code is working fine and this is the output of this code

enter image description here

enter image description here

But the problem is if the user only enters this

enter image description here

and this is the output

enter image description here

What I only want is to get the text field that is not empty.

1
  • 1
    why not just check empty value before push. if($(this).val() != '') selWeight.push($(this).val()); Commented Nov 5, 2019 at 6:35

2 Answers 2

1

This works fine thank you @Shree

   if($(this).val() != '') 
       selWeight.push($(this).val());
Sign up to request clarification or add additional context in comments.

Comments

1

Try to replace

$('input[name="myWeight"]').each(function() {
    selWeight.push($(this).val());
});

with next:

$('input[name="myWeight"]').each(function() {
    if ($(this).val() != '') {
        selWeight.push($(this).val());
    }
});

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.