I am using the following code:
$.each($('.friendName'), function(){
if($(this).val() != ''){
var idName = $(this).attr('id');
var name = $(this).val();
var email = $('#' + idName + ' ~ input').first().val();
alert(email);
}
});
This code should go through each element with a class of friendName (3 input boxes).
Ones that aren’t empty, I then want it to find the associated friendEmail input box. For example:
friendName1 - friendEmail1
friendName2 - friendEmail2
friendName3 - friendEmail3
This should only get results for items that contain details, if only friendName1 and friendEmail1 are filled out, it doesn’t count the rest of them.
For some reason, I can’t get it to get the value of the friendEmail input, I’m guessing because it is not a sibling.
Any help would be greatly appreciated, and if you could advance on my code so that it checks to see if each friendName and friendEmail contain values, then returns them as var name and var email, that’d be great.
<div>
<label for="friendName1">Friend's Name</label>
<input type="text" id="friendName1" name="friendName1" class="friendName" value="" />
</div>
<div>
<label for="friendEmail1">Friend's Email</label>
<input type="text" id="friendEmail1" name="friendEmail1" value="" />
</div>
<div>
<label for="friendName2">Friend's Name</label>
<input type="text" id="friendName2" name="friendName2" class="friendName" value="" />
</div>
<div>
<label for="friendEmail2">Friend's Email</label>
<input type="text" id="friendEmail2" name="friendEmail2" class="friendEmail" value="" />
</div>
<div>
<label for="friendName3">Friend's Name</label>
<input type="text" id="friendName3" name="friendName3" class="friendName" value="" />
</div>
<div>
<label for="friendEmail3">Friend's Email</label>
<input type="text" id="friendEmail3" name="friendEmail3" class="friendEmail" value="" />
</div>
I created a jsFiddle with an approach http://jsfiddle.net/a2kHt/2/