skeptic01 skeptic01 - 3 months ago 22
Javascript Question

navigating to prev element using jQuery

I am at the element with class fieldValidation(bolded below). And trying to navigate to the label field inside

.fTitleCell
.

Here's what I am doing.

$(".fieldValidation").each(function(){
console.log($(this).prev("fTitleCell").text);
});


The pattern of fTitleCell and fInputCell goes on for multiple fields and for each fInputCell, I need to traverse to the prev closest fTitleCell and grab its label.

<div class="fRow">

<div class="fTitleCell">
<input id="mainForm:j_idt139" type="checkbox" name="mainForm:j_idt139" class="checkbox-Access">
<label onmouseover="FA.fixLabelFor(this);">*IP Address*</label>
</div>

<span class="fInputCell searchField-ip_int" style="width:25.0%;">
<div xmlns="http://www.w3.org/1999/xhtml" xmlns:ga="http://gfs.com/">
<input type="text" name="mainForm:j_idt139" value="" class="fieldValidation" onblur="filter.setAdvCheckBox(this);" onkeyup="validation.checkType(this, 'ADDR');">
</div>
</span>

</div>

Answer

You need to go back to the parent. Then to the previous siblings element and after that look for the label.

JsFiddle as a working exemple.

$(".fieldValidation").each(function(){
     console.log($(this).parents('.fInputCell').prev('.fTitleCell').find('label').text())
});