SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
Jul 5, 2008, 06:52 #1
- Join Date
- Jun 2008
- Location
- Hyderabad
- Posts
- 252
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Find whether a Tag is present in between two tags or not
Suppose I have two tags <Date> and <Time> as below:
Code HTML4Strict:<Date> <Value>2008-07-07</Value> </Date> <TestTag> TestTag Text Node </TestTag> <Time> <Value>20:15:45</Value> </Time>
How can I find whether TestTag is present in between the <Date> and <Time> tags or not?
The <TestTag> may present any where in the document. But I want to know whether it is present between <Date> and <Time> tags or not?
-
Jul 5, 2008, 07:02 #2
- Join Date
- Apr 2006
- Location
- Maryland
- Posts
- 1,838
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
getElementsByTagName('date'), loop through all the Date elements and then inside of that check for getElementsByTagName('testtag') (length property)
-
Jul 5, 2008, 07:06 #3
- Join Date
- Jun 2008
- Location
- Hyderabad
- Posts
- 252
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
TestTag is not present in Date Tag...
-
Jul 5, 2008, 07:45 #4
- Join Date
- Aug 2007
- Location
- Brighton, UK
- Posts
- 2,006
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Something like this should work:
Code JavaScript:var TestTag = document.getElementsByTagName('TestTag'); for ( var i = 0 ; i < TestTag.length ; ++i ) { var next = (TestTag[i].nextSibling.nodeName=='#text') ? TestTag[i].nextSibling.nextSibling : TestTag[i].nextSibling; var previous = (TestTag[i].previousSibling.nodeName=='#text') ? TestTag[i].previousSibling.previousSibling : TestTag[i].previousSibling; if(next.nodeName=='Time'&&previous.nodeName=='Date') { alert('TestTag is present between date and time') } }
★ James Padolsey
–––––––––––––––––––––––––––––––––––––––
Awesome JavaScript Zoomer (demo here)
'Ajaxy' - Ajax integration solution (demo here)
-
Jul 5, 2008, 07:47 #5
- Join Date
- Jun 2008
- Location
- Hyderabad
- Posts
- 252
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks.
But TestTag is not the only tag that exists between Date and Time tags. in this case, what should I do?
-
Jul 5, 2008, 07:52 #6
- Join Date
- Aug 2007
- Location
- Brighton, UK
- Posts
- 2,006
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hmmm... You'd have to take a walk through the DOM in both directions. Tricky...
OR you could get the whole DOM into a string and then test it somehow to determine whether your pattern is there.
Getting the whole DOM:
Code JavaScript:function showDOM(){ var s = document.getElementsByTagName('*'); var str = ''; for(var i=0;i<s.length;i++) str+=s[i].nodeName+'<br>'; return; }
★ James Padolsey
–––––––––––––––––––––––––––––––––––––––
Awesome JavaScript Zoomer (demo here)
'Ajaxy' - Ajax integration solution (demo here)
-
Jul 5, 2008, 09:13 #7
- Join Date
- Dec 2007
- Posts
- 207
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If they are all in the same nest level just loop through the parent element, save the number of each, then check if the number of testtag is bigger than the number of Date and smaller than Time.
edit; example:
Code javascript:var i = 0 ,els = document.getElementsByTagName('parent').getElementsByTagName('*') ,l = els.length ,nodeN ,date ,testtag ,time; for ( ; i < l; i++){ nodeN = els[i].nodeName if (nodeN == "Date") date = i; if (nodeN == "TestTag") testtag = i; if (nodeN == "Time") time = i; } if (testtag > date && testtag < time) //do the do
mmj
-
Jul 8, 2008, 03:53 #8
- Join Date
- Jun 2008
- Location
- Hyderabad
- Posts
- 252
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Is there any easy way using jQuery?
-
Jul 8, 2008, 06:31 #9
- Join Date
- Dec 2007
- Posts
- 207
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Bookmarks