Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
this vs document.getElementById for fn calls?
Old 04-27-2007, 05:01 AM this vs document.getElementById for fn calls?
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Hi,

I'm having some trouble with a page I'm working on, and seeing the same thing in FF and IE ( 7 ), which means it's my fault. I'm starting to experiment with collapsible sections on some of my pages, sort of like nodes in a tree. It will definitely help people reading some of my articles, so I'm trying to figure out the best way to implement this. But javascript is my weak point.

My test page so far has been http://seattle-portraits.com/Tips.html

The first handful of sections get their own function, which is repetitive and just pointless. The last section ( location ) I'm tackling in a more generic way. Instead of hard coding IDs into DOM calls, I'm passing "this" as a parameter. In other words, from

function toggleMakeupSection() {
var a = document.getElementById("secMakeup");
to
function toggleSection(a, sectionDivID) {

When I make the change, this next bit stops working

if(a.innerHTML == "-") {
a.innerHTML = "+";

there's more, but that's the gist of it. When "a" is a locally declared variable that I look up with document.getElementById, I'm able to change the +/- symbol and things work the first time. The html is

<h2>[<a href="javascript:toggleSection(this, 'locationSection')" id="secLocation" style="color:red; font-weight:bold;" title="Click to hide">-</a>] Location</h2>

I can get it to work by passing the ID of the a element as a string instead, but there would be less maintenance overall if I could just pass the element as "this" and worse, I don't understand why that isn't working?

So ... can anybody give me any advice on this one?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
 
Register now for full access!
Old 04-27-2007, 06:01 AM Re: this vs document.getElementById for fn calls?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I find it best for cross browser, to pass "this.id" as the parameter value in the function call then get the object with getElementById(param) in the function.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to this vs document.getElementById for fn calls?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46217 seconds with 12 queries