0

I have this Javascript/JQuery code:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript">

    name = $("#name").val();

    alert("Name: " + name);

</script>

And this HTML:

<form name="report" action="send.php" method="post">

    <input type="text" name="name" id="name" value="Senna" />
    <input type="submit" value="Send" />

</form>

When the page loads i get "Name: undefined" instead of "Name: Senna".

Can someone help me to get the expected result?

4
  • 2
    Possible duplicate of everything ever... Commented Jul 3, 2012 at 21:56
  • Well... you make this mistake only once... better to get it out the way... Commented Jul 3, 2012 at 21:58
  • 1
    @Lix, You're right, as i am in my first steps with JQuery, this mistake i certainly won't make anymore Commented Jul 3, 2012 at 22:02
  • 1
    It really is the key to allowing you to get going with jQuery - good luck, happy coding and have fun! Commented Jul 3, 2012 at 22:03

3 Answers 3

2

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.

You need to indicate to execute the function once the page has been loaded. Currently you are only defining it and not decalring when to execute it.

$(document).ready(function(){
    name = $('#name').val();
    alert('Name: ' + name);
});

Or:

$(function(){
    name = $('#name').val();
    alert('Name: ' + name);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Very objetive your explanation! Thanks for the attention and patience!
Glad I could assist. I've learned over the years that providing the appropriate amount of details is very helpful in determining any solution. After all, it's hard to solve a problem if you don't understand what is happening.
2

You should place your code inside the DOM ready handler:

$(function() {
    var name = $("#name").val();
    alert("Name: " + name);
});

Otherwise you are trying the work with elements which are not yet loaded.

Comments

1

You need the document.ready callback.

Place your code inside a function like this -

$(function(){
  // code goes here
});

You need to use this callback to know when jQuery and the entire DOM is loaded. Only then can you start to manipulate the Markup and CSS with your jQuery code.


The code above is really just shorthand for this -

$(document).ready(function(){
  // code goes here
});

This uses the .ready() method - http://api.jquery.com/ready/
An extract from the description of the .ready() function -

... The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. ...

emphasis added

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.