I'm having problems getting this button to work here is the HTML

<head>
<script type="text/javascript" src="Objects.js"></script>
  <title></title>
</head>

<body>

<form>
<input type="button" value="Go Searching!" id="work_car" />

</form>

</body>

</html>

And here is the JavaScript:

function car(seats,engine,theradio) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
}
var work_car=new car ("cloth","V8","Tape Deck");

b1.onclick=work_car(){
document.write=("I want a car with +work_car.seats+ yeah");
};

var b1 = document.getElementById("work_car");

I want the button to display sentence by getting the name +work_car.seats+ from the Object, and it's not working.

Recommended Answers

All 24 Replies

you have to use the getElementById() AFTER the page has finished loading. Try:

function car(seats,engine,theradio) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
}

var work_car=new car ("cloth","V8","Tape Deck");
var b1=null;

window.onload=function(){
	b1 = document.getElementById("work_car");
	b1.onclick=function(){
		alert("I want a car with " + work_car.seats + " yeah");
		
		//if you put <div id="message"></div> in your document,
		//instead of the alert you  can use
		//document.getElementById("message").innerHTML="I want a car with " + work_car.seats + " yeah"
	};
};
Member Avatar for fatihpiristine

b1.onclick=function(){ ... } wont work on firefox

Hielo - I noticed you removed "window.alert" as I'm still learning I suppose you don't need the entire syntax window.alert ?

Also the alert should read "I want a car with cloth seats" how come the word seats is not being called ? Did I miss something ? Why did you put the null in there ?

I noticed you removed "window.alert" as I'm still learning I suppose you don't need the entire syntax window.alert ?

Correct. The "built-in" functions "belong" to the (global) window object, so you don't need to prefix it. Other examples: window.prompt(...), window.confirm(...). You can simply "drop" the window prefix.

Also the alert should read "I want a car with cloth seats" how come the word seats is not being called ? Did I miss something ?

You tell me. It works fine for me

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>hielo</title>
<head>
<script type="text/javascript">
var b1=null;
function car(seats,engine,theradio) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
}

var work_car=new car ("cloth","V8","Tape Deck");


window.onload=function(){
	b1 = document.getElementById("work_car");
	b1.onclick=function(){
		alert("I want a car with " + work_car.seats + " yeah");
		
		//if you put <div id="message"></div> in your document,
		//instead of the alert you  can use
		//document.getElementById("message").innerHTML="I want a car with " + work_car.seats + " yeah"
	};
};

</script>
  <title></title>
</head>

<body>

<form>
<input type="button" value="Go Searching!" id="work_car" />

</form>

</body>

</html>

Why did you put the null in there ?

I have a habit of initializing all variables.

Heilo you have the Javascript embedded into the HTML. This is my HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<script type="text/javascript" src="Objects.js"></script>
  <title></title>
</head>

<body>

<form><input type="button" value="Go Searching!" id="work_car" />
</form>

</body>

</html>

I'm calling an external JS file, and it should read

I want a car with cloth seats. Work_car.seats how come the word "seat" is not in the write command ?

function car(seats,engine,theradio) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
}

var work_car=new car ("cloth","V8","Tape Deck");
var b1=null;

window.onload=function(){
	b1 = document.getElementById("work_car");
	b1.onclick=function(){
		alert("I want a car with " + work_car.seats + " yeah");

Heilo you have the Javascript embedded into the HTML.

The name is Hielo.

Which browser are you using?

FireFox.

Hielo you have the Javascript embedded into the HTML...I'm calling an external JS file

It shouldn't make a difference, but just to humor you I tried the html code you posted and the exact js (except I added the two closing braces at the end of your onload function and it works fine. Here's my complete Objects.js file:

function car(seats,engine,theradio) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
}

var work_car=new car ("cloth","V8","Tape Deck");
var b1=null;

window.onload=function(){
	b1 = document.getElementById("work_car");
	b1.onclick=function(){
		alert("I want a car with " + work_car.seats + " yeah");
	};
};

Oh I see, I want it to say

"I want a car with cloth seats yeah"

There is nothing in the function to call that :)

Oh I see, I want it to say

"I want a car with cloth seats yeah"

There is nothing in the function to call that :)

function car(seats,engine,theradio,seats) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
this.sitting=sitting;
}

var work_car=new car ("cloth","V8","Tape Deck","seats");

How do I add the word seats in the instance "new car" for the write command as stated in previous messages?

Siberian,

First, put your brain in gear.

Then edit the following line to add the missing word:

alert("I want a car with " + work_car.seats + " yeah");

Airshow

I'm going to have to overview some reference material. Currently the button doesn't trigger no alert, so I have to check how to fix that line :)

Sib,

Hielo's code is cool. Go back and see what he did.

Airshow

The code works, the problem is I'm trying to figure out how to add "seats" in the alert command :)

function car(seats,engine,theradio,seats) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
this.sitting=sitting;
}

var work_car=new car ("cloth","V8","Tape Deck","seats");
var b1=null;

window.onload=function(){
	b1 = document.getElementById("work_car");
	b1.onclick=function(){
		alert("I want a car with " + work_car.seats+"yeah");
	};
};

I placed a object called sitting in the function then in the instance added "seats". Now I want to add "seats" to the alert.

Sib,

Forget about sitting. Delete it.

If you want the word "seats" in your message then add it to the line that generates the alert (line 14).

Add seats before the word yeah , inside the double-quotes.

Airshow

That works, but I'm forgetting something there no space between cloth and seats. HEre is the code.

b1.onclick=function(){
		alert("I want a car with " + work_car.seats+ "seats yeah");

Sib, if you want a space then add a space where you want it.

The message is yours to format exacctly the way you want it. You don't need anybody's permission to try out ideas.

Look at what is displayed then look at the code and work out what needs to be changed. That's the process - we call it "programming".

Airshow

I added a space and it won't reflect in the alert.

the space needs to go immediately to the left of the "s" in "seats": alert("I want a car with " + work_car.seats + " seats yeah");

the space needs to go immediately to the left of the "s" in "seats": alert("I want a car with " + work_car.seats + " seats yeah");

Hi Hielo, you're back. Whew!

Airshow

Hello to you too Airshow. I've been very busy lately, so I've been mostly off lately.

Bingo :) I knew it was something small. :)

Cool. The world can breath again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.