4

This will be a silly question, just trying to figure my way around Javascript functions interacting with HTML forms and can't get it to work.

Extremely simple, put it in a fiddle

$('#test').submit(function(){
    var battery = $('#battery').value();
    $('#output').text(battery);
});

<form id="test" action="">
    <fieldset>
        <label for="battery">Cell Count</label><br />
        <input type="number" size="3" name="battery" id="battery" /><br />
        <input type="submit" name="submit" id="submit" onclick="submit()" />
    </fieldset>
</form>

<div id="output">

</div>​​​​

​Always returns an error and I can't figure out why. Have played around with and other things, can't get it to work always error.

4
  • 1
    Since you're using jQuery you probably want to handle the click on your JS code and not inline in your HTML... Commented Dec 26, 2012 at 22:30
  • @elclanrs - Not only that, but a submit button will submit the form without any javascript all by itself. Commented Dec 26, 2012 at 22:34
  • Right. I've been searching Stack Overflow all morning and this is why I changed val() to value(), however it still returns error. I'm using the FF error console, but JSFiddle is just much easier for trial and error on something like this. Once I get the function to at least start I'll start in the browser. Thanks all. Commented Dec 26, 2012 at 23:00
  • Ok, well I've hosted it at bungle.ca/test.htm, gotten as far as an error on line 14 syntax error: illegal character. Commented Dec 26, 2012 at 23:53

6 Answers 6

4

The error is probably that .value() isn't a function. You want .val().

Sign up to request clarification or add additional context in comments.

Comments

1

jQuery's function for returning the value of an input element is val(), not value(). Your code should be:

$('#test').submit(function(){
    var battery = $('#battery').val();
    $('#output').text(battery);
});

<form id="test" action="">
    <fieldset>
        <label for="battery">Cell Count</label><br />
        <input type="number" size="3" name="battery" id="battery" /><br />
        <input type="submit" name="submit" id="submit" onclick="submit()" />
    </fieldset>
</form>

<div id="output">

</div>​​​​

Comments

1

If you use proper developer tools (for example Firebug in Firefox) then you could easily figure out the problem here as in the console it would have shown you something like :

Uncaught TypeError: Object [object Object] has no method 'value'

Which means that there is no value method and val() is what should be used instead.

Comments

0

To retrieve values from inputs using jQuery you need to use .val()

Change:

var battery = $('#battery').value();

to:

var battery = $('#battery').val();

Comments

0

I'm not sure if this is correct, but I think your javascript code looks like php, and there's nothing in the action="" field in the quotes. Also, I think that value should be val.

Comments

0

The correct function is val() no value() in jquery.

By using the method .submit keep in the "return false" to not run the "action" of the "form" that you have not considered.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $(document).on('ready', function() {
        $('#test').submit(function(){
            var battery = $('#battery').val();
            $('#output').text(battery);
            return false;
        });        
    })

</script>

If you are using jQuery, you do not need the onclick="submit()" argument in the input object

<form id="test" action="#">
    <fieldset>
        <label for="battery">Cell Count</label><br />
        <input type="number" size="3" name="battery" id="battery" /><br />
        <input type="submit" name="submit" id="submit"/>
    </fieldset>
</form>
<div id="output"></div>​​​​

regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.