Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jquery, $(this) , how to process form? 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I can't seem to work out how you process a form when you pass $(this) in the action property to a function

what is $(this) when in context of...
Code:
<form name="my_form" action="javascript:my_func($(this))">

in the old days 'this' used to be the form and you could then simply...

Code:
my_func(frm){

   alert(frm.myfield.value);

}

But this doesn't work anymore?

help understanding the object being passed in and how to access the value/pairs of the form is appreciated.

Thanks,
1DMF




"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
I just don't get JQuery and forms, this works..
Code:
my_func(){

    var frm = $('form.my_class');
    alert(frm.find('#email').val());

}

but passing in $(this) and then doing this doesn't ...

Code:
my_func(frm){
    alert(frm.find('#email').val());
}

They should be the same thing shouldn't they?


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
does this work

Code:
my_func(frm){
    alert($(frm).find('#email').val());
}
if so it is a node that is being passed, not a jQuery object.

<form name="my_form" action="javascript:my_func($(this))">

I'm no guru but I believe that this method is now frowned upon. the 'correct' method would be to do this (all wrapped inside a document.ready() block

Code:
jQuery('#formID').on('submit', function(e){
 //do some validation
 e.preventDefault(); //to stop form submitting for whatever reason
});

you can 'wrap' up all the form values that would be submitted by doing this

Code:
jQuery('#formID').on('submit', function(e){
 //do some validation
 e.preventDefault(); //to stop form submitting for whatever reason
 [red]var data = $(this).serialize();[/red]
 console.log(data);
});
 
I tried putting $(frm) , but that didn't work either, I just got 'undefined'. I guess $(this) isn't passing in the form as a JQuery object? not sure what it is passing to the function?

In the end I've gone with...
Code:
var frm = $('form.my_class');

Then .find() works as expected!

I'm no guru but I believe that this method is now frowned upon

Yes I know, but hey, turn that frown upside down ;-)

I am aware of the .serialize method, but I prefer to build by own AJAX query string, old fashioned I guess.

I'm actually using the JQuery validate plugin, so validation is all taken care of :)

As always, appreciate your input.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
as an aside : thought I'd give frm.serialize() a go and it works grand, so I guess there is no point in wasting my time with hand rolling the AJAX QS! , this old dog can learn a few new tricks yet [elephant2]

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
one advantage of hand-rolling the serialization is that you can pass a zero for unset checkboxes. but that's non-standard html behaviour and you'd have to code for it on the server side, and then remember two years later what you had done and why.

on balance i prefer the path of least resistance most of the time!
 
on balance i prefer the path of least resistance most of the time!
Agreed, but I am always torn between doing it the way I always have or using cargo cult or untested, unknown methods, plus as my employer always wants the project finished yesterday, sometimes doing it the way you know is quicker than trying to learn a new way!

Though that came and bit them in the behind this time with the WP template they commissioned, I'm just pleased I was able to widgetise the parts I needed with shortcodes (thanks to you of course [thumbsup2]), so I can finalise the new corporate rebranded website ready for rollout.

I've yet to 301 all the old ASP pages to the new WP pages, and cross fingers we don't disappear out the SE's! (I have a feeling they might be disappointed with the results!)

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top