I have this jQuery statement:
$(currentEle).parent().parent("li").next().click()
where currentEle is a li element.
If this part of the statement $(currentEle).parent().parent("li").next() returns null, I would like to add a .parent().parent("li") to the statement.
For example: $(currentEle).parent().parent("li").parent().parent("li").next().click() and so on.
I have added an HTML list to make things more clear. From the list, if the currentEle is set to the li representing E, and I would like to select the next element in the list i.e. ‘F’. $(currentEle).parent().parent("li").next()would be null and so $(currentEle).parent().parent("li").parent().parent("li") would do the job. So how do I make this more generic?
<ul>
<li>A</li>
<li>B
<ul>
<li>C
<ul>
<li>D
<ul>
<li>E</li>
</ul>
</li>
</ul>
</li>
<li>F</li>
</ul>
</li>
</ul>
Updated answer:
I added an
idto the Elito simplify the code.jQuery:
HTML:
JSFiddle DEMO