I am new to jquery and would like to know how do we check whether the input field has got an id or not..also whether the label reference is matching the input id?Please help me to do it. Right now the input id and the corresponding label reference are passed to the hidden value without any validation check
<html>
<head>
</head>
<body>
<form id="web2lead" action="">
<div>
<label for="txtName" class="addtocomment">Name </label>
<input type="text" id="txtName" class="addtocomment" name="txtName">
<label for="txtEmail" class="addtocomment">E-mail </label>
<input type="text" id="txtEmail" class="addtocomment" name="txtEmail">
<label for="txtAddress" class="addtocomment">Address</label>
<input type="text" id="txtAddress" class="addtocomment" name="txtEmail">
<input type=text name="hiddenbox" value="" id="hiddenbox">
<input type="submit" value="Go">
</div>
<script>
$(document).ready(function(){
$('#web2lead').on('submit', function(e){
e.preventDefault();
var txtData = [];
$('input.addtocomment').each(function(){
var text = $("label[for='"+this.id+"']").text() + $(this).val();
var numbersArray = text.split("\n");
txtData.push(numbersArray)
});
$("#hiddenbox").val(txtData);
var testdata=$("#hiddenbox").val();
alert(testdata);
});
});
</script>
</form>
</body>
</html>