简体   繁体   中英

Using onclick with javascript function, call js var in parameters

Hey guys a bit stuck here. Ill first start with showing you the code.

<script type="text/javascript" language="javascript">
var Stanley = "Text here";
function TextLooper(Alien)
{
var myArray = Alien.split("");
var loopTimer;
var changeLetter = function () 
{
    if(myArray.length > 0) 
    {
        document.getElementById("TypingStanley").innerHTML += myArray.shift();
    } 
    else 
    {
        clearTimeout(loopTimer); 
    }
}
setInterval(changeLetter, 70);
};
function InfoBoxClick(id, visibility,AlienVar) 
{
    document.getElementById(id).style.display = visibility; 
    TextLooper(AlienVar);
}
</script>

Thats my javascript and i use a href with onlick which is below;

<a href="#Aliens-Stanley" onclick="InfoBoxClick('StanleyInfo','block','Stanley');">LinkName</a>

I am trying for the third parameter of the function InfoBoxClick() to be the var named Stanley mentioned in the javascript, but i cant seem to do it, anyone know what i need to do???

You will need to reference Stanley as a variable in the onclick:

InfoBoxClick('StanleyInfo','block',Stanley);

JSFiddle

Just a friendly comment on style, variables should be lowercase , classes and constants should be CapitalCase and UPPERCASE respectively. It's only a convention, but will help other javascript developers understand your code.

bcoz in js one parameters is always hidden like (this),

if you want to pass 3 parameters then dont send 3. sent 4 parameters and 1st one is (this)

<a href="#Aliens-Stanley" onclick="InfoBoxClick(this,'StanleyInfo','block','Stanley');">LinkName</a>

and yore function write

function InfoBoxClick(e,id, visibility,AlienVar) 
{
    document.getElementById(id).style.display = visibility; 
    TextLooper(AlienVar);
}

in this 1st parameters e mens event of the action eg(onclick,......)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM