Get the value of a dynamic form field (JQuery)

advertisements

I have a form which uses dynamic inputs if the user chooses to show a div. I am unable to get the value from the dynamic form field.

Here's an example.

HTML (This div only displays when the user clicks the show DIV link)

<form id="myForm">

<input type="text" id="name" />

<a href="#" id="showDiv">Show Div</a>

<div id="newElement" style="display:none;"><input type="text" id="newField"/></div>

<input type="submit" name="submit" id="submit" />
</form>

Javascript - Let's assume the user has clicked the "Show Div" link which then shows the newElement div, and then submits the form. Here's the code that runs on submit.

$("#myForm").submit(function(){
      alert($('#name').val());
      alert($('#newField').val());
});

I receive a value for the first alert, but not the second.


My DIV was named the same thing as my form field (oops), stupid mistake. Thanks for taking a look though everyone.