/    Sign up×
Community /Pin to ProfileBookmark

Calling a function from .innerHTML

I have been trying to link a function to the .innerHTML tag. When the function is called it writes what I want it to, but erases my input button. If anyone can help me I would greatly appreciate it.

<html>
<head>
<script type=”text/javascript”>
var x;
var mycars = new Array();
mycars[0] = “Saab”;
mycars[1] = “Volvo”;
mycars[2] = “BMW”;

function cars()
{
for (x in mycars)
{
document.write(mycars[x] + “<br />”);
}
}

function change()
{
document.getElementById(‘Info’).innerHTML= cars();
}
</script>
</head>
<body>

<div id=”Info”>
<h2> Change This </h2>
</div>

<input type=”button” onclick=”change()” value=”Click me!”>
</body>
</html>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 07.2010 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;script type="text/javascript"&gt;
var mycars = [
"Saab",
"Volvo",
"BMW"
];
function cars()
{
var str='';
for (var x in mycars)
{
str+=mycars[x] + "&lt;br&gt;";
}
return str;
}

function change()
{
document.getElementById('Info').innerHTML += cars();
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div id="Info"&gt;
&lt;h2&gt; Change This &lt;/h2&gt;
&lt;/div&gt;

&lt;p&gt;&lt;input type="button" onclick="change()" value="Click me!"&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@KorJan 07.2010 — <i>
</i>function change()
{
document.getElementById('Info').innerHTML [COLOR="Red"]+=[/COLOR] cars();
}
[/QUOTE]

As far as I remember, IE won't like that. A safer approach could be:
<i>
</i>function change()
{
var txt=document.getElementById('Info').innerHTML;
txt+=cars();
document.getElementById('Info').innerHTML=txt;
}
Copy linkTweet thisAlerts:
@alexpndJan 07.2010 — I have been trying to link a function to the .innerHTML tag. When the function is called it writes what I want it to, but erases my input button. If anyone can help me I would greatly appreciate it.
[/QUOTE]


You need a "return" on the function, like Fang illustrated.
Copy linkTweet thisAlerts:
@FangJan 07.2010 — As far as I remember, IE won't like that. A safer approach could be:[/QUOTE] does work in IE. A safer approach is to use DOM methods

You need a "return" on the function, like Fang illustrated.[/QUOTE]
There's a little more to it than that ?
×

Success!

Help @sexybeastkid spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...