Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
Your code seems to be fine: jsfiddle.net/nivas/zewyU – Nivas Aug 3 '12 at 3:14
@Nivas the code is not working there, as you see it says the word stanley, but in that code it should say text here. I have now sorted this problem, my next problem is if i click more than once it calls the function multiple times and alters the array – Wibberley1991 Aug 3 '12 at 4:07

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
That works perfect thank you, ill accept this. One more question, is it possible to tell it to only run the script once as getting a bug when i click on the link again – Wibberley1991 Aug 3 '12 at 3:11

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

share|improve this answer
doesnt seem to answer my question, but what is the benefit of putting this? and the reason for it? – Wibberley1991 Aug 3 '12 at 11:58
if you dont put "this" and your last parameter i.e "3rd" automatically convert in "this", "this" mens even name – Kanaiya Aug 3 '12 at 12:05
first run puting with "this" and check it, then tell me – Kanaiya Aug 3 '12 at 12:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.