1

I'm trying to get value of input field which is present in <td> tag.

<td class="edit_td1" id="<?php echo $id; ?>">
 <div class="form-group">
 <div class="col-lg-3 inputContainer">
 <input class="form-control cls-quantity" id="quanti_<?php echo $id; ?>" name="number" type="text" />
 </div>
 </div>
</td>
//...some code here
<a href="customer_details.php?shop=<?php echo $_GET['shop'];?>" class="btn btn-info" role="button" onclick="return td_validation();">Generate Bill</a>

and javascript code is:

function td_validation()
{
 //alert("tds validation");
 var tds = document.getElementById('table_id').getElementsByTagName('td');
 var sum = 0;
 //alert(tds.length);
 for(var i = 0; i < tds.length; i ++)
 {
 if(tds[i].className == 'edit_td1' && tds[i].innerHTML==0)
 {
 //alert(tds[i].innerHTML);
 sum +=i;
 }
 }
 if(sum !=0)
 {
 alert("Enter quantity for "+sum+" fields");
 return false;
 }
 else
 {
 return true;
 }
}

Here I want to get value in input field and if this value is 0 then do sum +=i.

Rory McCrossan
338k41 gold badges320 silver badges354 bronze badges
asked Sep 3, 2015 at 9:40

2 Answers 2

2

Here is a shorter version of what are you trying to do:

function td_validation()
{
 var tds = document.querySelectorAll('#table_id td.edit_td1');
 var sum = 0;
 for(var i = 0; i < tds.length; i++)
 {
 if (tds[i].querySelector('input.cls-quantity').value === "0") {
 sum +=i;
 }
 }
 if(sum !=0)
 {
 alert("Enter quantity for "+sum+" fields");
 return false;
 }
 else
 {
 return true;
 }
}
answered Sep 3, 2015 at 9:48
Sign up to request clarification or add additional context in comments.

1 Comment

great...it works. Please vote up if my question is helpful.
0

@toivin's answer is correct, I did some modifications in it,

function td_validation()
{
 var tds = document.getElementById('table_id').getElementsByTagName('td');
 var sum = 0;
 for(var i = 0; i < tds.length; i ++)
 {
 if(tds[i].className == 'edit_td1' )
 {
 var a = parseInt(tds[i].querySelector('input.cls-quantity').value);
 if(a==0)
 {
 sum +=i;
 }
 }
 }
 if(sum !=0)
 {
 alert("Quantity should not be zero...!!!");
 return false;
 }
 else
 {
 return true;
 }
}
answered Sep 3, 2015 at 10:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.