最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript function won't fire on button click - Stack Overflow

programmeradmin2浏览0评论

I've just started teaching myself JavaScript and am doing some basic exercises. Right now I'm trying to make a function that takes in three values and prints out the largest. However, the function refuses to fire upon button click. I tried making a separate, very simple function just for debugging and it refuses to fire as well. I've written other practice functions in almost the exact same manner (they had two parameters as opposed to three) that work fine. Any insight would be greatly appreciated.

<!DOCTYPE HTML>
<html>
<head>
<script type = "text/javascript">
<!--

//Define a function Max() that takes three numbers as 
//arguments and returns the largest of them.
function Boo(){
    document.write("boo");
    alert("boo");
}


function Max(p1, p2, p3){
    document.write(p1+p2+p3);
    alert('BOO!')

    document.write(document.getElementById('value1').value);

    var max = Number(p1);
    v2 = Number(p2):
    v3 = Number(p3);    

    if (v2 > max)
        max = v2;
    if (v3 > max)
        max = v3;

    document.write(max);
}
-->
</script>
    </head>
    <body>
<form>
    <input type="text" id="value1" name="value1"/><br />
    <input type="text" id="value2" name="value2"/><br />
    <input type="text" id="value3" name="value3"/><br />
    <input type = "button" 
    onclick="Boo()" 
    value = "Compare!"/>
    <!-- onclick="Max(document.getElementById('value1').value,
                 document.getElementById('value2').value,
                 document.getElementById('value3').value)" -->
</form>
  </body>
 </html>

I've just started teaching myself JavaScript and am doing some basic exercises. Right now I'm trying to make a function that takes in three values and prints out the largest. However, the function refuses to fire upon button click. I tried making a separate, very simple function just for debugging and it refuses to fire as well. I've written other practice functions in almost the exact same manner (they had two parameters as opposed to three) that work fine. Any insight would be greatly appreciated.

<!DOCTYPE HTML>
<html>
<head>
<script type = "text/javascript">
<!--

//Define a function Max() that takes three numbers as 
//arguments and returns the largest of them.
function Boo(){
    document.write("boo");
    alert("boo");
}


function Max(p1, p2, p3){
    document.write(p1+p2+p3);
    alert('BOO!')

    document.write(document.getElementById('value1').value);

    var max = Number(p1);
    v2 = Number(p2):
    v3 = Number(p3);    

    if (v2 > max)
        max = v2;
    if (v3 > max)
        max = v3;

    document.write(max);
}
-->
</script>
    </head>
    <body>
<form>
    <input type="text" id="value1" name="value1"/><br />
    <input type="text" id="value2" name="value2"/><br />
    <input type="text" id="value3" name="value3"/><br />
    <input type = "button" 
    onclick="Boo()" 
    value = "Compare!"/>
    <!-- onclick="Max(document.getElementById('value1').value,
                 document.getElementById('value2').value,
                 document.getElementById('value3').value)" -->
</form>
  </body>
 </html>
Share Improve this question asked May 31, 2012 at 14:33 DanielDaniel 2,5155 gold badges27 silver badges41 bronze badges 4
  • Does Boo() work? If not then check if JS is enabled in your browser – mariosk89 Commented May 31, 2012 at 14:38
  • Well in your max function you're missing semi-colon at the end of the second line which would cause it to fail. – Nigel B Commented May 31, 2012 at 14:38
  • You should use var for all your variables, like v2 and v3, to avoid implicitly using globals in javascript (multiple instances of Max or other functions with the name v2 will overwrite each other's values). – ninjagecko Commented May 31, 2012 at 14:39
  • For future reference, Math.max(1,2,3) -> 3 – ninjagecko Commented May 31, 2012 at 14:40
Add a ment  | 

4 Answers 4

Reset to default 5
  v2 = Number(p2):

should be

v2 = Number(p2);

Also, look into the developer tools for whatever browser you are using to find simple syntax errors like this. (In most browsers you can press F12).

chrome developer tool says it has syntax error!

v2 = Number(p2):

should be:

v2 = Number(p2);

Your javascript functions are mented out with <!-- -->

This is all you need:

    var elements = document.getElementsByName('to_pare');
    var elementsAsArray = [].slice.call(elements);
    var numberValues = elementsAsArray.map(function(x) {
        return Number(x.value)
    });
    return Math.max.apply(this, numberValues);

Full demo, including using console.log rather than document.write, also including how to append to document without clearing it: http://jsfiddle/yYhjD/1/

<head>
    <script type="text/javascript">

function pare() {
    var elements = [].slice.call(document.getElementsByName('to_pare'));
    var numberValues = elements.map(function(x){return Number(x.value)});
    return Math.max.apply(this, numberValues);
}

    </script>
</head>

<body>
    <input type="text" id="value1" name="to_pare" value="-10"/>
    <input type="text" id="value2" name="to_pare" value="080"/>
    <input type="text" id="value3" name="to_pare" value="79"/>
    <br/>
    <input type="button" onclick="console.log(pare())" value="Compare!"/>
</body>
发布评论

评论列表(0)

  1. 暂无评论