I just can't get this Javascript

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: May 2004
Posts: 133
Reputation: Kamex is an unknown quantity at this point 
Solved Threads: 0
Kamex's Avatar
Kamex Kamex is offline Offline
Junior Poster

I just can't get this Javascript

 
0
  #1
Aug 11th, 2005
I'm familiar with HTML and CSS, and have now started to learn PHP without running into any trouble, but I want to add Javascript to my knowledge. I tried a long time ago, and failed, for the same reason I'm failing now. Code that looks logical to me is not logical in Javascript. Anything I try to make on my own always results in some kind of error, even if it appears to make sense to my warped brain.

What I'm trying to do now should be fairly simple. I'm trying to create a test page with a navbar and a set of links. When the user hovers over the links, the text should change to a discription of that link. I have altered my code a great deal, and have at least accomplished creating the discription (even if the CSS for it is in serious need of adjustment). However, no matter how many different things I try, I cannot get the discription to actually change. Firefox's Javascript console will either give me an error, or nothing will happen at all.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html> <!--HTML Transitional Javascript Test Page-->
  3.  
  4. <!--Header Information-->
  5. <head>
  6.  
  7. <!--Page's Title-->
  8. <title>
  9. Test Page
  10. </title>
  11.  
  12. <!--CSS Styling Information-->
  13. <style>
  14. /*Class Specific Styling Information*/
  15. div#navbar
  16. {
  17. background-color: #00FF00;
  18. position: absolute;
  19. top: 0px;
  20. left: 0px;
  21. width: 100%;
  22. }
  23.  
  24. div#navLinks
  25. {
  26. float: left;
  27. }
  28.  
  29. div#linkDescriptions
  30. {
  31. background-color: #FFFF88;
  32. margin: 10px 10px 10px 10px;
  33. width: 100%;
  34. }
  35. </style>
  36.  
  37. <!--Scripting Information-->
  38. <script type="text/javascript">
  39. //Create the Link Discriptions div (Done using Javascript so that it doesn't appear with Javascript disabled).
  40. function creatediscription()
  41. {
  42. var navbar = document.getElementById('navbar')
  43. var linkDescriptions = document.createElement('div');
  44. linkDescriptions.id = "linkDescriptions";
  45. var linkDescriptions_textNode = document.createTextNode('Hover over a menu item for the discription.');
  46. linkDescriptions.appendChild(linkDescriptions_textNode);
  47. navbar.appendChild(linkDescriptions);
  48. }
  49.  
  50. function changetext()
  51. {{
  52. linkDescriptions_textNode.nodeValue = 'Test';
  53. alert('that aint it');
  54. }}
  55. </script>
  56.  
  57. </head>
  58.  
  59. <!--Body Information-->
  60. <body onload="creatediscription();">
  61. <div id="navbar">
  62. <div id="navLinks">
  63. <p>
  64. <a href="#" onmouseover="changetext();">Main</a>
  65. </p>
  66. </div>
  67. </div>
  68. </body>
  69.  
  70. </html>

Despite having syntax highlighting, there must be something seriously wrong in here, because on the second function, I got an error saying there was a missing curly brace even when there was one. So I added a second one, which resulted in a complaint about a missing end brace. After adding a second brace, the error went away, so now I'm stuck with a rather rediculous double-bracketed function.

Note that the alert() is there because I was using it to debug to see if the function was even being called at all. It looks like it isn't, although considering that I have to put two brackets around it to avoid errors, that's not surprising.

I've been puzzling over this code for 2 and a half days now, and I'm ready to call it quits again. Can someone help?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: I just can't get this Javascript

 
0
  #2
Aug 11th, 2005
I'm sure a few of us can take a crack at it. However, what you're trying to dol really isn't typical. Hyperlinks should be self-descriptive:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <a href="order_processing.php">Complete your order</a>

"Complete your order" describes what the hyperlink does. Now, I can imagine a scenario where you would like to give your users a fuller explanation of what some hyperlinks do. But physically changing the text of the hyperlink isn't a solution I'd consider. I would

1) set aside an "explanation" portion of the page, perhaps as a <div> element.
2) when the user hovers over a link, change the contents of the <div> element to explain what that link does.

or, alternatively, provide a help system that explains each link, and reference it near the links:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. If you're not sure what to do next,
  2. please refer to the <a href="help.html">online help system</a>.
  3.  
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: I just can't get this Javascript

 
0
  #3
Aug 11th, 2005
The main problem you have is understanding variable scope. When you use the keyword "var" INSIDE A FUNCTION, you are declaring that the variable is local, or scoped to that function. That being the case, your "changetext" function cannot access the objects you create in your "creatediscription" function.

Examine the code I have below, which is what you're trying to achieve, I think. I have removed the "var" keyword, making those objects global in scope.

I've also modified the "changetext" function to use "documentGetElementById" and the "innerHTML" property, to illustrate their use.

Have fun!

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html> <!--HTML Transitional Javascript Test Page-->
  4.  
  5. <!--Header Information-->
  6. <head>
  7.  
  8. <!--Page's Title-->
  9. <title>Test Page</title>
  10.  
  11. <!--CSS Styling Information-->
  12. <style>
  13. /*Class Specific Styling Information*/
  14. div#navbar
  15. {
  16. background-color: #00FF00;
  17. position: absolute;
  18. top: 0px;
  19. left: 0px;
  20. width: 100%;
  21. }
  22.  
  23. div#navLinks
  24. {
  25. float: left;
  26. }
  27.  
  28. div#linkDescriptions
  29. {
  30. background-color: #FFFF88;
  31. margin: 10px 10px 10px 10px;
  32. width: 100%;
  33. }
  34. </style>
  35.  
  36. <!--Scripting Information-->
  37. <script type="text/javascript">
  38. //Create the Link Descriptions div.
  39. function createdescription()
  40. {
  41. navbar = document.getElementById('navbar')
  42. linkDescriptions = document.createElement('div');
  43. linkDescriptions_textNode =
  44. document.createTextNode('Hover over a menu for a description.');
  45. linkDescriptions.id = "linkDescriptions";
  46. linkDescriptions.appendChild(linkDescriptions_textNode);
  47. navbar.appendChild(linkDescriptions);
  48. }
  49.  
  50. function changetext()
  51. {
  52. document.getElementById("linkDescriptions").innerHTML = "Test.";
  53. }
  54. </script>
  55.  
  56. </head>
  57.  
  58. <!--Body Information-->
  59. <body onload="createdescription();">
  60. <div id="navbar">
  61. <div id="navLinks">
  62. <p>
  63. <a href="#" onmouseover="changetext();">Main</a>
  64. </p>
  65. </div>
  66. </div>
  67. </body>
  68.  
  69. </html>
  70.  
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: I just can't get this Javascript

 
0
  #4
Aug 25th, 2005
You never followed-up. Did you my post make sense? How are your JavaScript efforts going?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC