Solved

x.textContent error

Posted on 2014-07-21
5
140 Views
Last Modified: 2014-07-21
I have a js file and I'm playing around with object literals but it's not working.

Any ideas why?

Thanks!
0
Comment
Question by:Isaac
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:duncanb7
ID: 40210575
From the information you provide, I think it is because x element  has error. If you can provide more x element information, it will assist to solve your issue.

Duncan
0
 
LVL 5

Author Comment

by:Isaac
ID: 40210581
The file was attached but here you go....

<!DOCTYPE html>
<html>

<head>
	<title></title>
	
	<script type="text/JavaScript">
		var hotel = {
			name: 'Sunset',
			rooms: 65,
			Booked: 15,
			checkAvailability: function() {
				return this.rooms - this.Booked;
			}
		};
		
		var elName = document.getElementById('hName');
		elName.textContent = hotel.name;
		
		var rmLeft = document.getElementById('hRoomLeft');
		rmLeft.textContent = hotel.checkAvailability();		
	</script>

</head>

<body>
	<table>
		<tr>
			<td>Hotel Availability</td>
		</tr>
		<tr>
			<td>Hotel: </td>
			<td><div id="hName"></div></td>
		</tr>
		<tr>
			<td>Rooms Left: </td>
			<td><div id="hRoomLeft"></div></td>
		</tr>				
	</table>
</body>
</html>

Open in new window

0
 
LVL 13

Accepted Solution

by:
duncanb7 earned 500 total points
ID: 40210612
it is because the html page is not loaded completely before javascript running,

 please try this by adding init() function with onload in body tag

Duncan
<!DOCTYPE html>
<html>

<head>
	<title></title>
	
	<script type="text/JavaScript">
	function init(){
		var hotel = {
			name: 'Sunset',
			rooms: 65,
			Booked: 15,
			checkAvailability: function() {
				return this.rooms - this.Booked;
			}
		};
		
		var elName = document.getElementById('hName');
		elName.textContent = hotel.name;
		
		var rmLeft = document.getElementById('hRoomLeft');
		rmLeft.textContent = hotel.checkAvailability();	
}		
	</script>

</head>

<body onload="javascipt:init();">
	<table>
		<tr>
			<td>Hotel Availability</td>
		</tr>
		<tr>
			<td>Hotel: </td>
			<td><div id="hName"></div></td>
		</tr>
		<tr>
			<td>Rooms Left: </td>
			<td><div id="hRoomLeft"></div></td>
		</tr>				
	</table>
</body>
</html>

Open in new window


Or you try this , change script location
<!DOCTYPE html>
<html>

<head>
	<title></title>


</head>

<body>
	<table>
		<tr>
			<td>Hotel Availability</td>
		</tr>
		<tr>
			<td>Hotel: </td>
			<td><div id="hName"></div></td>
		</tr>
		<tr>
			<td>Rooms Left: </td>
			<td><div id="hRoomLeft"></div></td>
		</tr>				
	</table>
		<script type="text/JavaScript">
		var hotel = {
			name: 'Sunset',
			rooms: 65,
			Booked: 15,
			checkAvailability: function() {
				return this.rooms - this.Booked;
			}
		};
		
		var elName = document.getElementById('hName');
		elName.textContent = hotel.name;
		
		var rmLeft = document.getElementById('hRoomLeft');
		rmLeft.textContent = hotel.checkAvailability();		
	</script>
</body>
</html>

Open in new window

0
 
LVL 5

Author Closing Comment

by:Isaac
ID: 40210631
Thanks! That's something I should have known.
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40210642
Thanks for your points

Have a nice day

Duncan
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now