0

I select some element with .someClass and I want to select next <label> element which occurs lower in the DOM tree.

Between those two elements can be anything else, e.g:

<input class="someClass" />
<p>asdfasdf</p>
<div></div>
<table>
    <tr>
        <td>
            <label>SOMETHING</label>
        </td>
    </tr>
</table>

My script:

 $(".question").each(function (index, value) { 
     vargroupNumber = ++index;
     $(":radio", this)
     .attr("name", "group" + groupNumber)
     .each(function (index, value) {        
         $(this)
         .removeAttr('checked')
         .attr("id", "id" + groupNumber + index)
         .next('label') 
         .‌​attr("for", "id" + groupNumber + index); 
     }); 
 });

I want to select and set something to this <label>.

Michał Rybak
  • 8,648
  • 3
  • 42
  • 54
  • Have you tried anything? – j08691 Oct 25 '13 at 16:43
  • i did many things :D It works for predefined layout but other than that it's complicated... Actually my case is bit more complex, this is how it works only if –  Oct 25 '13 at 16:44
  • post code sample of what you've tried so far and what isn't working. – Latheesan Oct 25 '13 at 16:45
  • @LatheesanKanes i just did –  Oct 25 '13 at 16:46
  • on what basis you want to select ``, does it share the same class name with other element(s) on DOM or what? – Udit Bhardwaj Oct 25 '13 at 16:46
  • just because it appears under down the DOM tree from current one...on that basis –  Oct 25 '13 at 16:47
  • why don't you set a class to your label, like this: `` - now you can set content to it like this: `$('.resultLabel').html('what-ever-you-want');` – Latheesan Oct 25 '13 at 16:48
  • in that case i would not bother anyone to help me :) not possible... –  Oct 25 '13 at 16:49
  • possible duplicate of [Jquery: Forget the DOM structure, just find the next element with this class](http://stackoverflow.com/questions/12873027/jquery-forget-the-dom-structure-just-find-the-next-element-with-this-class) – Michał Rybak Oct 26 '13 at 01:46

4 Answers4

0

you can do this by Recursion here is the code

var elem = $('.someClass');

findlabel(elem)

function findlabel(elem) {

if (elem.prop('tagName') == 'LABEL') {
    elem.text('yahoo');
    //do what you want to do
} else if (!elem.is(':empty')) {

    var notfound = 1;
    elem.find('*').each(function () {

        if ($(this).prop('tagName') == 'LABEL') {
            $(this).text('yahoo');
            //do what you want to do
            notfound = 0;
            return false;
        }

    });

    if (notfound == 1) {
        elem = elem.next();
        findlabel(elem);
    }

} else {
    elem = elem.next();
    findlabel(elem);
}
}

and here is the working example:- http://jsfiddle.net/LLkdA/

i hope this is what you want

Udit Bhardwaj
  • 1,761
  • 1
  • 19
  • 29
0

This has been already solved (and even "pluginified").

Community
  • 1
  • 1
Michał Rybak
  • 8,648
  • 3
  • 42
  • 54
  • I really don't understand how come that they didn't make this part f jquery already. This is bit abstract but can be very useful. –  Oct 26 '13 at 05:26
-1

Try

$(".someClass").parent("body").find("label");

Instead of the body use whatever the parent of someClass and table is.

Fiddle

bhb
  • 2,476
  • 3
  • 17
  • 32
-1

Your definately going to want to use Jquery's next function. I asked basically the same question and got my answer

Getting Div ID on Header Click, Accordion

Community
  • 1
  • 1
CSharper
  • 5,420
  • 6
  • 28
  • 54