Jump to content

[SOLVED] New To Javascript


Northern Flame

Recommended Posts

I hardly program in Javascript and when I do its simple stuff,

but I am stuck on something. I want to have a few links where

if the user click them, something is displayed in a certain div.

Heres a simple sample that I was practicing with so that you

guys can better understand what I am trying to do. Can anyone

help me make this work?

 

<script type="text/javascript">
function secondDiv(){
document.write('This is the default text....');
}
function numberOne(){
function secondDiv(){
document.write('Number One');
}
}
function numberTwo(){
function secondDiv(){
document.write('Number Two');
}
}
</script>
<div style="background-color:#CC0000;">
<a onClick="numberOne()" href="javascript:void(0)">Number One</a><br>
<a onClick="numberTwo()" href="javascript:void(0)">Number Two</a><br>
</div>
<div>
<script type="text/javascript">
secondDiv();
</script>
</div>

 

it displays "This is the default text...." but the text doesnt change,

maybe I am doing this wrong, am I suppose to do it another way

other than changing the function that is being displayed?

Link to comment
https://forums.phpfreaks.com/topic/79931-solved-new-to-javascript/
Share on other sites

don't use document.write in a function; I have never seen that turn out good. do it like this:

 

 

<script type="text/javascript">
function numberOne()
{
document.getElementById('content').innerHTML='Number 1';
}
function numberTwo()
{
document.getElementById('content').innerHTML='Number 2';
}
</script>
<div style="background-color:#CC0000;">
<a onClick="numberOne()" href="javascript:void(0)">Number One</a><br>
<a onClick="numberTwo()" href="javascript:void(0)">Number Two</a><br>
</div>
<div>
<span id="content">
This is the default text....
</span>
</div>

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.