t0mcat
t0mcat

Reputation: 5679

Javascript sample program not working

I am trying to run a sample javascript code from the link: Link

Nothing happens on FF and on IE, after scanning, it just clears out the text field. What is the issue here?

Code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="javascript">
    function ini()
    {

    // Retrieve the code
    var code =document.getElementById ('code_read_box').value;

    alert(code);

    // Return false to prevent the form to submit
    return false;

    }

</script>

</head>


<body>

    <form onsubmit = "return ini()">

    <input type="text" id="code_read_box" value="" />

    </form>
</body>
</html>

Upvotes: 2

Views: 223

Answers (3)

Mark Bolusmjak
Mark Bolusmjak

Reputation: 24409

<script type="text/javascript">

instead of

<script type="javascript">

Upvotes: 4

Šime Vidas
Šime Vidas

Reputation: 185923

This works:

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
</head>
<body>

    <form onsubmit="return ini()">
        <input type="text" id="code_read_box">
    </form>

    <script>
        function ini() {
            var code = document.getElementById('code_read_box').value;
            alert(code);
            return false;
        }
    </script>

</body>
</html>

The same thing but with use of jQuery:
(this will make it work in IE)

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
</head>
<body>

    <form id="foo">
        <input type="text" id="code_read_box">
    </form>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#foo").submit(function() {
                alert( $("#code_read_box").val() );
                return false;
            });
        });
    </script>

    </body>
</html>

Upvotes: 0

zigdon
zigdon

Reputation: 15063

"The Issue" can be any one of a multitude of things. Are you sure the JS code is being loaded? Is there an error in the JS console? Have you tried to use something like firebug to see what's actually happening?

Without more information, there's no way we can guess what's actually going on.

Upvotes: 0

Related Questions