views:

85

answers:

3

Hi Folks,

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"&gt;
<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>
A: 

"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.

zigdon
Hi Zigdon, I posted the code. Not sure how to use Firebug
t3ch
+4  A: 

<script type="text/javascript">

instead of

<script type="javascript">

z5h
+1 - good catch - http://jsfiddle.net/f5PtF/
Peter Ajtai
You can ditch the type attribute entirely...
Šime Vidas
A: 

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"&gt;&lt;/script&gt;
    <script>
        $(document).ready(function() {
            $("#foo").submit(function() {
                alert( $("#code_read_box").val() );
                return false;
            });
        });
    </script>

    </body>
</html>
Šime Vidas
Hi Sime Vidas, What was wrong with the posted code? I see you removed value from the input tag.
t3ch
The type="javascript" attribute of the SCRIPT element. Just use type="text/javascript" or do not set the type attribute altogether (I don't)
Šime Vidas
Hi Sime, The code you posted didnt work either.
t3ch
My code works in FF, Safari, Chrome and Opera. I only have IE9 beta here and it works in that browser only after the 3rd submit. In what browsers doesn't it work for you?
Šime Vidas