Jump to content
Sign in to follow this  
fikiwan

How to make a Global Varibale in JavaScript...

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...