Jump to content

Comparing states of an object


george

Recommended Posts

I have some JavaScript code here. I'm not sure why it does not work. Comparing states of the object objName

		<script type="text/javascript">			var obj;			function expand(objName) { 				obj = document.getElementById(objName).style;				if ( obj.display='none' ) {					obj.display = 'block';				} 				if ( obj.display='block' ) {					obj.display = 'none';				} 			}		</script>

Here is the HTML part. Look for the two instances of "Bname".

	<caption class="caption"><a href="#" id="tab1" onclick="expand('Bname');">Appetizers</a></caption>		<tbody bgcolor="#FFFFFF" id="Bname">			<tr><td>Shrimp Roll</td><td>1 for $1.25</td><td>2 for $2.50</td></tr>

Link to comment
Share on other sites

Problem solved Thanks

			function expand(objName) { 				obj = document.getElementById(objName).style;				if ( obj.display=='none' || obj.display==null ) {					obj.display = 'block';				} else {					obj.display = 'none';				} 				return true;			}

Link to comment
Share on other sites

At 56 years old, I find that all IT hiring is going to children. Ok, ~tween 20-30yo children. I'm sure health care coverage and assured quick wittedness are significant factors. I can't blame those hiring for using those criterion. Just means I have no choice but to freelance. Peace - out

Link to comment
Share on other sites

it could be an issue with familiarity of newer languages, techniques, conventions, etc. Ultimately though your profile/resume should speak for itself. Of course, its cheaper to hire someone new for less money than to pay someone with lots of experience in the field (vs. recent formal education) what they should be paid/are worth.

Link to comment
Share on other sites

Have you read Joel Spolsky's "Smart and gets things done" a guide for IT companies HR practices? He makes the point that seasoned programmers can fill in the experience gap of the freshly graduated. Most depressing book I have ever read. Oh, and speaking of depressed, I still have a problem. The site I am working on will open and close a table body, as I wanted, but only after an initial dud click. IOW, I have to click twice on the "Appetizers" box to get the table to appear. From then on, I can click "Appetizers" and the table will expend and compress as I wanted. It behaves as though the obj in

obj = document.getElementById(objName).style;

is not initialized until I have looped through the whole function once. I tried initializing the variable outside the function, but this did not resolve the problem. The problem may also be in how I call the function, or the juxtaposition of the elements I am using. Thanks

Link to comment
Share on other sites

try just checking for display == "none" and/or removing the return. Unfortunately I haven't found a way to change Javascript at runtime w/Firebug yet...although using breakpoints indicates the correct conditions are being checked for (i.e. block, none)

Link to comment
Share on other sites

My way of using break points is to insert an alert('message') into my javascript. Probibly not the most efficent. I have been using Firefox's firebug and error consol and DOM inspector. Can I set break points with one of these?

Link to comment
Share on other sites

I think the style properties are initially set to '' (at least some of them are, display being one of them) until JavaScript changes it, regardless of what is specified in CSS. So maybe try testing for an empty string.Edit:Yes, you can set breakpoints in the Script tab in Firebug.

Link to comment
Share on other sites

I think the style properties are initially set to '' (at least some of them are, display being one of them) until JavaScript changes it, regardless of what is specified in CSS. So maybe try testing for an empty string.Edit:Yes, you can set breakpoints in the Script tab in Firebug.
ah! good catch. It is worth putting an alert statement after your document.getElementById() line so you can see what is being checked each time the function is run (and specifically for the first time), so you know exactly what value that method is returning initially, like so:
<script type="text/javascript">function expand(objName) {  obj = document.getElementById(objName).style;  alert("elements display value: " + obj.display);  (obj.display == ('none' || '')) ? obj.display = 'block' : obj.display = 'none';  //same thing as an if/else statement*   return true;  }</script>

*if each condition only has one line of executable code

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...