The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> JavaScript Global Variable Question?
scorks
post Mar 11 2015, 10:49 PM
Post #1





Group: Members
Posts: 7
Joined: 16-January 15
Member No.: 22,040



So I have the following JS function:

CODE

var globalTD;

function toggleOverlay(element){
    globalTD = element;
    
    var overlay = document.getElementById('overlay');
    var specialBox = document.getElementById('specialBox');
    overlay.style.opacity = .8;
    if(overlay.style.display == "block"){
        overlay.style.display = "none";
        specialBox.style.display = "none";

    } else {
        overlay.style.display = "block";
        specialBox.style.display = "block";
    }
}

function addText() {
    var localTD = globalTD;
    var text = document.getElementById('textToAdd').value;
    localTD.innerHTML += text;
    toggleOverlay();
}


and it works fine, but the "element" that I pass in to my toggleOverlay function is actually a TD element. I want to store the original contents of the TD element using another global variable, to use it in the deleteText function like this:

CODE

var globalTD;
var global2;

function toggleOverlay(element){
    globalTD = element;
    global2 = element.innerHTML;
    var overlay = document.getElementById('overlay');
    var specialBox = document.getElementById('specialBox');
    overlay.style.opacity = .8;
    if(overlay.style.display == "block"){
        overlay.style.display = "none";
        specialBox.style.display = "none";

    } else {
        overlay.style.display = "block";
        specialBox.style.display = "block";
    }
}

function addText() {
    var localTD = globalTD;
    var text = document.getElementById('textToAdd').value;
    localTD.innerHTML += text;
    toggleOverlay();
}

function deleteText() {
    var localTD = global2;
    localTD.innerHTML = localTD;
    toggleOverlay();

}


But it won't work for some reason. I believe it has something to do with my "element.innerHTML" line in the "toggleOverlay" function, but I'm not sure why it won't work. Any suggestions?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
scorks
post Mar 11 2015, 11:16 PM
Post #2





Group: Members
Posts: 7
Joined: 16-January 15
Member No.: 22,040



Nevermind, I've figured out a different way of doing it using substrings. Thanks anyways!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 12 2015, 12:25 AM
Post #3


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



I think your original problem was because of this line: 'localTD.innerHTML = localTD;'. localTD comes from global2 and global2 only contains the innerHTML of the cell, not the element object. You should have used 'globalTD'.innerHTML, as long as you are still working with the same element.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 12 2015, 10:42 AM
Post #4


.
********

Group: WDG Moderators
Posts: 9,663
Joined: 10-August 06
Member No.: 7



I'll add that no element parameter is passed when you call toggleOverlay() from inside addText() or deleteText():

CODE

function addText() {
    toggleOverlay();
}

resulting in a script error when toggleOverlay() tries to access element.innerHTML.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 5th May 2024 - 03:28 AM