2

Stripping the code to bare essentials, I have a form

name="addPageForm" id="addPageForm" method="get" 

and I have an input type="text"

name="categoryTxt" id="categoryTxt" 

This is my script

<script> var strData = $('#addPageForm').serialize(); alert(strData); </script> 

The alert shows

categoryTxt= 

Where am I going wrong?

6
  • You've stripped a bit too much. What's the value of your textbox, and are you sure you just don't have a typo like "<inptu" ? Commented Apr 1, 2014 at 6:34
  • I guess you are submitting form on pageLoad when your "categoryTxt" is empty that's why alert shows nothing... you can do two thing 1) submit form after input in your textbox OR 2) set default value into your textbox Commented Apr 1, 2014 at 6:53
  • no you can not update this way, you ask another question read stakeoverflow ASK QUESTION FAQ. Commented Apr 1, 2014 at 6:58
  • Thanks @user3321823. I had since resolved that problem of form not getting serialized by submitting the form after textbox input. The question stands edited now. Commented Apr 1, 2014 at 7:00
  • @user1153551 I am sorry. I was not aware of this. Commented Apr 1, 2014 at 7:01

2 Answers 2

1

Why Unable to serialize and how to i serialize - Reason behind your alert box popup when page load so at that time your input is empty so alert empty value, But you can try trigger way when you click submit to alert your form data by this following way,

Check this demo jsFiddle

jQuery

$( "form" ).on( "submit", function( event ) { event.preventDefault(); var strData = $('#addPageForm').serialize(); console.log(strData); alert(strData); }); 

HTML

<form name="addPageForm" id="addPageForm" method="get"> <input name="categoryTxt" id="categoryTxt" /> <input type="Submit" name="Submit" /> </form> 

Hope this help you!

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

1 Comment

Thanks @user1153551. I figured that out just as soon as I hit the Ask Question button. :p Question edited.
1

The textfield doesn't have a value, therefore the field is displayed as empty.

<form name="addPageForm" id="addPageForm" method="get"> <input type="text" name="categoryTxt" id="categoryTxt" value="text" /> </form> <script> var strData = $('#addPageForm').serialize(); alert(strData); </script> 

will show

categoryTxt=text 

1 Comment

Thanks @akirk. I figured that out just as soon as I hit the Ask Question button. :p Question edited.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.