I'm writing a search function in JavaScript and am having some troubles with it. Currently, I'm using the help of jQuery for ease-of-use on my momentarily retarded brain.
The function I'll be using as a starting point will, indeed, be using the jQuery library. However, I would much rather know how to do this in JavaScript, so code responses in either vanilla or jQuery will most certainly suffice. This code, after all, is going into a large jQuery plugin I'm writing.
So, the function is supposed to search the current page for a DOM element containing a specified class and return it only after it has passed up a specified element.
Do explain this further:
Code:
I want to scan <article> to find the next element with the class `myclass` starting from the element with either id `a`, `b`, or `c` (which ever one I pass through the function).
Static variables:
Code:
The above function will do the trick... only if the start element is one of the immediate children of the <article> element.
[edit] My Ramblings...
The function I'll be using as a starting point will, indeed, be using the jQuery library. However, I would much rather know how to do this in JavaScript, so code responses in either vanilla or jQuery will most certainly suffice. This code, after all, is going into a large jQuery plugin I'm writing.
So, the function is supposed to search the current page for a DOM element containing a specified class and return it only after it has passed up a specified element.
Do explain this further:
Code:
<html>
<body>
<article>
<header>
<h1 class="myclass" id="a">An Article's Title!</h1>
<span>Posted at 00.00 on January 0th, 3034</span>
</header>
<p class="myclass" id="b">BLARGH BLARGH BLARGH</p>
<p class="myclass">Lorem Ipsum</p>
<blockquote><p class="myclass" id="c">Dolor sit amit</p></blockquote>
<p class="myclass">Lipsum</p>
</article>
</body>
</html>
I want to scan <article> to find the next element with the class `myclass` starting from the element with either id `a`, `b`, or `c` (which ever one I pass through the function).
Static variables:
- Class name,
- <article> (element to begin search in)
- Start element (either ID: `a`, `b`, or `c`),
- The element found
Code:
function nextMyClass(obj) {
if(obj==false) temp = $('article').children()[0];
else temp = obj;
while((typeof temp).toUpperCase()=="OBJECT") {
if(temp===startElement) {
$flag = true;
} else if($flag===true) {
if($(temp).hasClass('myclass')) {
return temp;
} else if((typeof temp.children()[0]).toUpperCase()=="OBJECT") {
temp2 = nextMyClass(temp.children()[0]);
if(temp2!==false) return temp2;
}
}
temp = temp.next();
}
return false;
}
[edit] My Ramblings...
Quote:
I think the problem right now is: "How do I pass the flag?!"... Because if I can pass the flag then the current function will work... But I don't want to clutter the code with more function params containing the current flag... Or would that be the logical way? Just add another argument that carries the flag? But the other problem is retrieving the flag without ruining the default output of OBJECT or BOOL... Unless of course I assign the flag to an element in the object returned (whether false or the object.flag = true|false)
I just don't think it has to be that complicated... am I wrong? :/
I just don't think it has to be that complicated... am I wrong? :/