473,400 Members | 2,163 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,400 software developers and data experts.

Need Help Changing Table post load with Javascript

Hi Folks,

I seem to have quite a conundrum here and I'm at my wits end. Essentially I have a HTML table where I'm trying to place a message at the top of the table should a value appear later in the table.

My code is basically as follows...

Expand|Select|Wrap|Line Numbers
  1. ...
  2. <table>
  3. <script language="javascript">
  4.       var myval = getmyval();
  5.       if(myval == "True") {
  6.            document.write("<tr><td>My Val is True</td></tr>");
  7.       }
  8. </script>
  9.  
  10. <tr><td id="my_val" name="my_val">True</td></tr>
  11. <script language="javascript">
  12.       function getmyval() {
  13.             return document.getElementById('my_val').innerHTML;
  14.       }
  15. </script>
  16. </table>
  17.  
  18. ...
The code errors out with "Object Expected" on the "var myval = germyval();" call.

This makes me think that the function getmyval() needs to appear before the call, however if I do that I run into the getElementById call failing since that value isn't present at declaration.

Can anyone help me solve this?
Ryan
Aug 3 '07 #1
2 1280
Hi Folks,

I seem to have quite a conundrum here and I'm at my wits end. Essentially I have a HTML table where I'm trying to place a message at the top of the table should a value appear later in the table.

My code is basically as follows...

Expand|Select|Wrap|Line Numbers
  1. ...
  2. <table>
  3. <script language="javascript">
  4.       var myval = getmyval();
  5.       if(myval == "True") {
  6.            document.write("<tr><td>My Val is True</td></tr>");
  7.       }
  8. </script>
  9.  
  10. <tr><td id="my_val" name="my_val">True</td></tr>
  11. <script language="javascript">
  12.       function getmyval() {
  13.             return document.getElementById('my_val').innerHTML;
  14.       }
  15. </script>
  16. </table>
  17.  
  18. ...
The code errors out with "Object Expected" on the "var myval = germyval();" call.

This makes me think that the function getmyval() needs to appear before the call, however if I do that I run into the getElementById call failing since that value isn't present at declaration.

Can anyone help me solve this?
Ryan
The problem is as you have stated. You are just seeing a secondary manifestation of the same problem. When getmyval() is called it doesn't exist.
When getmyval() exists, the element itself doesn't exists.

Solution: create a hidden div on the page:

[HTML]
<div id="myvalStatus" style="display:none;"></div>
[/HTML]

Then, your javascript should run in a window.onload handler. This ensures that it won't try to execute until all the code and page elements are in place.

Something like this (caution: untested):

Expand|Select|Wrap|Line Numbers
  1. <head>
  2.     <script type="text/javascript">
  3.         function setMyvalStatus() {
  4.             var myval = document.getElementById('my_val').innerHTML;
  5.             if(myval == "True") {
  6.                 var myvalDiv = document.getElementById('myvalStatus');
  7.                 myvalDiv.innerHTML = "It's True!";
  8.                 myvalDiv.style.visibility = "";
  9.                 myvalDiv.style.display = "";
  10.             }
  11.         }
  12.     </script>
  13. </head>
  14. <body onload="setMyvalStatus()">
  15.  
  16.     ...  html goes here ...
  17.  
  18. </body>
  19.  
Aug 3 '07 #2
pbmods
5,821 Expert 4TB
Thanks for using CODE tags! Did you know that you can specify a language for your CODE tags to make your source code easier to read?

You will still need to use [/code] to close your code blocks, regardless of the language, but you can the use one of these tags to open your code block:

[code=html]
[code=javascript]
[code=php]

and so on.

Thanks!

MODERATOR
Aug 4 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Don Grover | last post by:
I have a table thats wrapped in a div tag, that when user selects 1 of 2 radio buttons it hides or shows table, this works ok. But I want to set the table show hide on what the existing state of...
31
by: Greg Scharlemann | last post by:
Given some recent success on a simple form validation (mainly due to the kind folks in this forum), I've tried to tackle something a bit more difficult. I'm pulling data down from a database and...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
3
by: moondaddy | last post by:
I have some text in a page that when a certain even happens, I need to make it glow from black to red (through a spand of 1 second) pause for a few seconds, and then slowly change from red to black...
0
by: dnphamus13 | last post by:
I'm new to this and drowning right now. I would like to put my database online for viewing. I managed to do the filtering but i need to do PAGING as the XML doc get bigger. From what i understand...
3
by: RC | last post by:
Dear Dudes, I post this in multiple groups for opening brain storm. Sometime I need to query the data from database server then display them into user's browser in HTML <table>. But if the...
9
by: dli07 | last post by:
Hello, I'm trying to convert a piece of code that creates a dynamic vertical resizing bar in a table from internet explorer to firefox. It's based on a post from...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
4
by: gubbachchi | last post by:
Hi all, Please anybody help me solve this problem. I am stuck up with this from past 2 weeks. I am developing an application where, when the user selects date from javascript datepicker and enters...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.