Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-20-2010, 07:21 PM   PM User | #1
wtmh
New to the CF scene

 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
wtmh is an unknown quantity at this point
Correct placement of code using insertBefore/After

I'm brand new to JavaScript, and I come from a purely logical scripting background so forgive me if this is the most poorly formed code/bad technique that you've recently seen.

I've played the google games for days and I surrender. I usually find a way to fight through it and figure it out, but I'm going to have to cede this one.

I'm simply trying to put a new tab next to a tab that is already present on a page (classed: mirrortab_first)

I'm trying to use XPath to select the element and then place newTab before it.

Seemed simple enough to start. But as the code sits, the tab always winds up at the bottom of the page.

Any thoughts?

Kind regards.

Code:
var newTab = document.createElement("div");
newTab.setAttribute('id','newTab');

newTab.innerHTML =
'<table cellpadding="0" cellspacing="0" border="0"><tbody><tr>' +
'<td class="mirrortab_first">&nbsp;</td>' +
'<td class="mirrortab_back">' +
'<a style="cursor:pointer;">Hide Classifieds</a>' +
'</td>' +
'<td class="mirrortab_last">&nbsp;</td>' +
'</tr></tbody></table></div>';


var tabLocation = document.evaluate('//td[contains(@class, "mirrortab_first")][1]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
for (i=0; i<tabLocation.snapshotLength; i++)
	{
		tL = tabLocation.snapshotItem(i).parentNode;
	}

document.body.insertBefore(newTab, document.tL);
wtmh is offline   Reply With Quote
Old 08-20-2010, 08:38 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ummm...document.evaluate() is *NOT* supported in all browser, at all. Certainly not in MSIE.

But in any case, you areo doing
Code:
document.body.insertBefore(newTab, document.tL);
but tL is a simple variable, not a member of document. So presumably document.tL is null and so the placement you are getting is expected.

Did you try simply using
Code:
document.body.insertBefore(newTab, tL);
???

But also maybe you should find a way to do this that doesn't depend on evaluate( ).

If you showed us the web page, it would be easier to help.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-20-2010, 09:56 PM   PM User | #3
wtmh
New to the CF scene

 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
wtmh is an unknown quantity at this point
I have tried
Code:
document.body.insertBefore(newTab, tL);
Which results in the tab not showing at all.

I don't have a finished page since I'm just doing this on a blank throw away document just to see if I can do it. Again, I'm learning.

What ways would I be able to trace a complex path to an element without using evaluate? I'm all about compatibility. :P
wtmh is offline   Reply With Quote
Old 08-20-2010, 10:18 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, the easiest thing to do would be to put a class name on each of your tabs.

And then just use getElementsByClassName() and insert the new div before/after the one you are after. It's possible, of course, to look inside the tabs for the <td> with that class, but just slower and more of a pain.

On really old browsers, getElementsByClassName doesn't exist, but it's easy to write it using getElementsByTagName and then inspecting each element found for the desired class.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
insert html, insertbefore, xpath

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:14 AM.


Advertisement
Log in to turn off these ads.