473,378 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

simple javascript problems

Hi,

I am relatively new to javascript, and I am having issues with using the
document.getElementById function. I am building a class and in the
constructor, I associate the object to an element in the page by passing
the string id of the element into the constructor function. Then I
assign this.element = document.getElementById(the string). However,
when I try to access this.element, the console tells me it has no
properties. I have tried to use the getElementById elsewhere in the page
to fix the problem, but it won't recognize the element IDs. I am not
sure what i am doing wrong! I appreciate any help that can be given.
Below is the test page with code. Thanks!

-Kirk

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head></head>
<script language="JavaScript" type="text/javascript">

function textCycler(elementID){//constructor for textCycler
this.element = document.getElementById(elementID);//document
element in which text will be cycled
this.text = new Array();//text array
this.pages = 0;//count of pages of text
this.textPointer = -1;//points to currently viewed text in text array
}

textCycler.prototype.getCurrentText = function(){
return (this.textPointer==-1)? ("") : this.text[this.textPointer];
}

textCycler.prototype.update = function(){
this.element.innerHTML = this.getCurrentText();
}

textCycler.prototype.moveForward = function(){
if(this.pages > 0){
this.textPointer = (this.textPointer + 1) % this.pages;
this.update();
}
alert(this.textPointer);
}

textCycler.prototype.moveBackward = function(){

if(this.pages > 0){
this.textPointer = (this.textPointer == 0)? (this.pages - 1) :
(this.textPointer - 1);
this.update();
}

}

textCycler.prototype.loadText = function(textArray){
this.text = textArray;
this.pages = this.text.length;
this.textPointer = 0;
}

textCycler.prototype.loadFirst = function(){

if(this.pages > 0 && this.textPointer > -1){
this.textPointer = 0;
this.update();
}

}
testArray = ["a","b","c","d","e","f"];
testCycler = new textCycler('testy');
testCycler.loadText(testArray);
testCycler.loadFirst();
//alert(document.getElementById("testy").innerHTML);
</script>
<body>
<a href="#" onClick="testCycler.moveBackward();">*--back</a>&nbsp;
&nbsp; <a href="#" onClick="testCycler.moveForward();">forward --*</a>
<div name="testy" id="testy">
asdlfkjasdflkjasdlf
</div>
</body>
</html>
Jul 23 '05 #1
1 1415
Hi Kirk,

First of all, this is why your script doesn't work. You are trying to
create an object with an elementID that does not exist. I know you can
see that it exists, but the browser has yet to see it. The browser is
busy executing your script. Only after it has finished rendering the
page, does it know that the elementID exists when you do a
document.getElementById() call.

Second, don't use objects in your event handler. i.e. onClick =
"obj.method", because it does not know what obj is. This mainly has to
do with scoping. Instead make a generic function which invokes that
object's method:

<a href = "#" onClick = "moveBackward();">back</a>
<a href = "#" onClick = "moveForward();">forward</a>

In your script:

function moveBackward()
{
testCycler.moveBackward();
}

function moveForward()
{
testCycler.moveForward();
}

Finally, to fix your problem, do the following instead:
var testCycler;

function init()
{
testCycler = new textCycler('testy');
testCycler.loadText(testArray);
testCycler.loadFirst();
}

window.onload = init;

This way after the page has loaded you can guarantee that the call for
document.getElementById() will work.

Hope this helps. :)

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
7
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I...
5
by: gray_slp | last post by:
I am designing a web survey using surveymonkey.com and discovered I could use javascript to modify their standard question formats much the same as can be done in myspace. I used this feature to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.