Ask Question Forum:
Model Library:2025-02-08:A.I. model is online auto reply
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-01-11 04:08:09
Point:500 Replies:4 POST_ID:828879USER_ID:11059
Topic:
jQuery;;JavaScript
There're two forms without any id attribute help that I want to.
I want to get html() for all class name ='mesg' in first form only when first form is
onsubmit . I try the following chkvalid() code, but it shows all classname="mesg" even
including second form. the output is "0:A","1:B","2:C","3:D" from console.log
I just want "0:A", "1:B" within first form only .
function chkvalid( obj) {
$( ".mesg" ).each(function( index ) {
console.log( index + ": " + $( this ).html() ); });
So now I try to fix the issue from getting the correct jquery selector such as
$(obj).$(".mesg").each(function( index ) {
console.log( index + ": " + $( this ).html() ); });
but it fail since it is not allowed to use $(obj) and then point to other object inside of
parent object.
How to do that ? and please don't ask me to add id attribute in each form to distinguish both forms.
Please advise
Duncan
I want to get html() for all class name ='mesg' in first form only when first form is
onsubmit . I try the following chkvalid() code, but it shows all classname="mesg" even
including second form. the output is "0:A","1:B","2:C","3:D" from console.log
I just want "0:A", "1:B" within first form only .
function chkvalid( obj) {
$( ".mesg" ).each(function( index ) {
console.log( index + ": " + $( this ).html() ); });
So now I try to fix the issue from getting the correct jquery selector such as
$(obj).$(".mesg").each(function( index ) {
console.log( index + ": " + $( this ).html() ); });
but it fail since it is not allowed to use $(obj) and then point to other object inside of
parent object.
How to do that ? and please don't ask me to add id attribute in each form to distinguish both forms.
Please advise
Duncan
<htm><body><form onsubmit="chkvalid(this);return false;"><table><tr><td class='mesg'>A</td></tr><tr><td class='mesg'>B</td></tr><tr><td><button type='submit'><span>Submit</span></button></td></tr></table></form><form onsubmit="chkvalid(this);return false;"><table><tr><td class='mesg'>C</td></tr><tr><td class='mesg'>D</td></tr><tr><td><button type='submit'><span>Submit</span></button></td></tr></table></form></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:
Author: duncanb7 replied at 2024-01-11 07:06:10
Thanks for your reply
have a nice day
Duncan
have a nice day
Duncan
Expert: leakim971 replied at 2024-01-11 06:44:23
Question-1:
Why did you add << return false >> here :
<form onsubmit="chkvalid(this);return false;">
I see no "CONDITIONAL" logic to submit OR NOT the form as it always do << return false >>
Q2 : this represent a context, you can use $(this).find(".mesg") instead
$(this, ".mesg") represent all the form inside an element with mesg class, non-sense
Q3 :
why not but as jQuery slogan is write less do more...
$(".mesg", this)
vs :
$(this).find(".mesg")
16 vs 21 characters
Why did you add << return false >> here :
<form onsubmit="chkvalid(this);return false;">
I see no "CONDITIONAL" logic to submit OR NOT the form as it always do << return false >>
Q2 : this represent a context, you can use $(this).find(".mesg") instead
$(this, ".mesg") represent all the form inside an element with mesg class, non-sense
Q3 :
why not but as jQuery slogan is write less do more...
$(".mesg", this)
vs :
$(this).find(".mesg")
16 vs 21 characters
Author: duncanb7 replied at 2024-01-11 06:36:49
Thanks for your reply, leakim971
Question-1:
Why I need this to prevent return false ?
event.preventDefault(); // prevent submission of the form like << return false; >>
onsubmit(chkvalid();return false); where "return false" by that I can do submit form
on form action after doing chkvalid javascript function
Question-2
What is meaning of $(".mesg",this), where this I understand that is refered to the submit object, but what is sequence meaning of $(".mesg", this) ? and why not $(this,".mesg") ?
Question-3
$(obj ).find(".mesg").each(function( index) {
console.log( index + ": " + $( this ).html() );
});
I used $(obj). find() to get it work finally , is it same as your method of $(".mesg",this) ?
I will try your code since I feel find() is not good since it will do some loop seach and
it might not need it since each() will handle this. So the question is finding a good selector , Right ?
Please advise
Duncan
Question-1:
Why I need this to prevent return false ?
event.preventDefault(); // prevent submission of the form like << return false; >>
onsubmit(chkvalid();return false); where "return false" by that I can do submit form
on form action after doing chkvalid javascript function
Question-2
What is meaning of $(".mesg",this), where this I understand that is refered to the submit object, but what is sequence meaning of $(".mesg", this) ? and why not $(this,".mesg") ?
Question-3
$(obj ).find(".mesg").each(function( index) {
console.log( index + ": " + $( this ).html() );
});
I used $(obj). find() to get it work finally , is it same as your method of $(".mesg",this) ?
I will try your code since I feel find() is not good since it will do some loop seach and
it might not need it since each() will handle this. So the question is finding a good selector , Right ?
Please advise
Duncan
Accepted Solution
Expert: leakim971 replied at 2024-01-11 05:49:34
500 points EXCELLENT
In your code :
Use : $( ".mesg", obj ).each(function( index ) {
instead : $( ".mesg" ).each(function( index ) {
Test page : http://jsfiddle.net/UPNLv/
Use : $( ".mesg", obj ).each(function( index ) {
instead : $( ".mesg" ).each(function( index ) {
Test page : http://jsfiddle.net/UPNLv/
<html><head><script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><script> jQuery(function($) { $("form").submit(function(event) { event.preventDefault(); // prevent submission of the form like << return false; >> $( ".mesg", this ).each(function( index ) { console.log( index + ": " + $( this ).html() ); }); }); });</script></head><body><form><table><tr><td class='mesg'>A</td></tr><tr><td class='mesg'>B</td></tr><tr><td><button type='submit'><span>Submit</span></button></td></tr></table></form><form><table><tr><td class='mesg'>C</td></tr><tr><td class='mesg'>D</td></tr><tr><td><button type='submit'><span>Submit</span></button></td></tr></table></form></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34: