de1337ed
de1337ed

Reputation: 3325

jquery and dynamic forms help?

I'm very new to jquery, and I'm trying to make a dynamic form in the following manner (and its not working). I want the form to have an initial value of "hello" as stated in the header region.

I understand that you can do this easily with html (with the value parameter), but I have a much larger goal in mind that requires the value to be dynamic.

Anyway, I just can't seem to get it to work, so help please. Thank you.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Logging In Test</title>
    <script type = "text/javascript" src="{{ STATIC_URL }}js/jquery.js">
    var value = "hello";
    $('#login [name=firstname]').val(value)
    </script>
</head>
<body>
<form id="login">
    <input type = "text" name="firstname">
</form>
</body>
</html>

Thank you.!

Upvotes: 0

Views: 155

Answers (2)

Marek Sebera
Marek Sebera

Reputation: 40651

use $(document).ready(); http://api.jquery.com/ready/
like

$(document).ready(function(){
    var value = "hello";
    $('#login [name=firstname]').val(value)
});

to set initial values after document fully loads and DOM is created properly

Upvotes: 2

red-X
red-X

Reputation: 5128

there is a problem with how and when you insert jquery try the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Logging In Test</title>
</head>
<body>
<form id="login">
    <input type = "text" name="firstname">
</form>



<script type = "text/javascript" src="{{ STATIC_URL }}js/jquery.js"></script>
<script type = "text/javascript">
    var value = "hello";
    $('#login [name=firstname]').val(value)
</script>
</body>
</html>

Upvotes: 0

Related Questions