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.
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.