1

Need to know how to apply a variable to a javascript function. I'm currently trying to get my code to work but it's not recognizing my value as a function type.

    <!DOCTYPE html>
<html>
<body onload="myFunction()">

<select>
    <option>1</option>
</select>
<br/>
<select>
    <option>1</option>
</select>

<script>
function myFunction()
{
    if(typeof document.body.ontouchstart == "undefined"){actionIn = "onmouseover"; actionOut = "onmouseout"}
    else{actionIn = "ontouchstart"; actionOut = "ontouchend";}
    document.write(actionIn+" "+actionOut);
    var elem = document.getElementsByTagName("SELECT");
    for (var i = 0;i < elem.length; i++){
        elem[i].actionIn = function(){this.style.background='red';}
        elem[i].actionOut = function(){this.style.background='';}
    }
}
</script>

</body>
</html>

1 Answer 1

3

You're trying to write

elem[i][actionIn] = ...;
Sign up to request clarification or add additional context in comments.

3 Comments

SyntaxError: missing name after . operator [Break On This Error] elem[i].[actionIn] = function(){this.style.background='red';}
SyntaxError: missing name after . operator [Break On This Error] elem[i].[actionIn] = function(){this.style.background='red';}
@user2309465: Yes; you should not have a .. (just like [i])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.