Jump to content

How to make a Global Varibale in JavaScript...


fikiwan

Recommended Posts

Hey all .. I do a tes for make a global varibel in Javascriptfirst time I try like it

var satu = 10;var dua = 5;function hitung(){document.write(satu+dua);}

no problem so far,.. and I try for second like this: I want display a tag hidden with action onClick ,

var mun = document.getElementById("mun");function muncul(){ mun.style.display ="block";}<a href="#" onClick="muncul();">Tes For Muncul</a><div id="mun" style="display:none;">Saya Akan Muncul Disini Baby</div>

its not work so I try for the third like this:

function muncul(){var mun = document.getElementById("mun");mun.style.display ="block";}<a href="#" onClick="muncul();">Tes For Muncul</a><div id="mun" style="display:none;">Saya Akan Muncul Disini Baby</div>

and it's run very well ; So , I had a question whether a tag can't be for a global Variabel , cause I have need variable same for any function, Thanks :)

Edited by fikiwan
Link to comment
Share on other sites

When the window loads, you have to set your global variables to what you want them to be set to, so that they can be accessible to functions etc. So for example:

<script type="text/javascript">var mun;     window.onload = function()     {        mun = document.getElementById("mun");     }     function muncul()    {        mun.style.display = "block";    }</script>

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...