Declaring and using variables in Javascript

Hi all,

I’m a javascript newbie and having some trouble with what I imagine it s a very basic issue!

I am trying to declare a variable inside a function and use it later on in my code… but it just already returns white space… i.e. not variable value.

I am setting it within this function:

function show_video1(){
	document.getElementById('video1').style.display="block";
	var video1Name = "Education World News (Part 1)";
	document.getElementById('video2').style.display="none";
	document.getElementById('video3').style.display="none";
	document.getElementById('video4').style.display="none";
	document.getElementById('video5').style.display="none";
	document.getElementById('video6').style.display="none";
	document.getElementById('video7').style.display="none";
	document.getElementById('video8').style.display="none";
	document.getElementById('video9').style.display="none";
	document.getElementById('video10').style.display="none";
	document.getElementById('video11').style.display="none";
}

and trying to call it later on with this:


<script type="text/javascript">document.write(video1Name)</script>

It might be worth noting that each one of my 11 videos will hace a different name.

Can anyone help out a newbie?

Many thanks,

greens85

Hi blue,

Sorry to be a pain but I have now amended my code to that and still got no joy!

Do I need to define anything on the link:


<div class="nextVideo"><a href="javascript:show_video1()" alt="Play Video" title="Play Video">Education World News (Part 1)</a></div>
<div class="nextVideo"><a href="javascript:show_video2()" alt="Play Video" title="Play Video">Education World News (Part 2)</a></div>

Not sure if it makes things easier but a live example can be found here:

http://www.education-world.co.uk/Education%20World%20Site/index.php

You are declaring the variable video1Name within your function.
The scope of visibility will be that function and that function only.

If you want to declare a variable, do something to it with a function and then later on use it somewhere else, you do it like this:


var some_variable = 'test';

function do_something()
{
    some_variable = 'this is updated';
}

// call the function and change contents of "some_variable"

do_something();

alert(some_variable); // alerts 'this is updated'

This example should be enough for you to make the change to your code easily.

No, not really… you redeclared “videoName” in your show_video2() function.
After you declare variable with “var” keyword, it becomes visible globally.
However, you re-declared it within your function.

In your show_video2() function use just “videoName = ‘Education World News (Part 2)’;” without var before variable.

Hi Blue,

Many thanks for your reply…

I can now get the default value of the variable to show, but when I call the second function the variable value doesn’t change!

I’m pretty sure I have followed your instructions to a T but must have got something wrong…

var videoName = 'Education World News (Part 1)'; // Declared outside of any function
function show_video2(){
	var videoName = 'Education World News (Part 2)'; // Attempting to change the value here
}
Now Playing: <script type="text/javascript">document.write(videoName)</script> // trying to show the variable value here, I know you mentioned alert but presumably you don't have to use alert and that was just an example.