SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
May 17, 2007, 02:30 #1
- Join Date
- Jan 2007
- Posts
- 14
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
use js functions only for particular forms
I have a nice little js script for using XHR to submit forms and get the results back in hidden div. My problem is that it currently is setup to work with all forms on the page. I only want it to work particular forms. Here is my code
http://pastebin.ca/492590
Im thinking i need to change line 85 and then add action on the submit buttons on the forms i want to use it with. Im really horrible at js, so any help is really appreciated.
-
May 17, 2007, 04:57 #2
- Join Date
- Sep 2002
- Location
- Bournemouth, South UK
- Posts
- 1,551
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
you could either reference the form by ID or class
if you want to apply the functions to one form then use ID, but you could set a function that iterates through all the forms, and see if one has the className of, for instance, jsapply, and if one is found apply the function.
Hope it's of helpLiveScript: Putting the "Live" Back into JavaScript
if live output_as_javascript else output_as_html end if
-
May 17, 2007, 08:27 #3
- Join Date
- Jan 2007
- Posts
- 14
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 17, 2007, 08:33 #4
- Join Date
- Sep 2002
- Location
- Bournemouth, South UK
- Posts
- 1,551
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Code:function findClass() { var x=document.getElementsByTagName('form'); for(i=0;i<x.length;i++) { if(x[i].className.indexOf("class_named_form")>-1) { do_something(); } } }
LiveScript: Putting the "Live" Back into JavaScript
if live output_as_javascript else output_as_html end if
-
May 17, 2007, 22:04 #5
- Join Date
- Jan 2007
- Posts
- 14
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks for the sample code, but i wasnt able to get it to work. Here is part of the original code.
Code:// Initialize forms: window.onload = ajaxInit; function ajaxInit(){ for (var i = 0; i < document.forms.length; i++){ var curForm = document.forms[i]; if (curForm.method == "post") { curForm.onsubmit = post_method; // Apply post_method to *every* form on the page } else if (curForm.method == "get") { curForm.onsubmit = get_method; } } } <form action="" method="post" id="{$ip}chk"> <input type="hidden" value="revSubmit.php" name="resultspage" id="resultspage" /> <input type="hidden" value="check" name="mode" /> <input type="hidden" value="{$ip}" name="ip" /> <input type="submit" value="Check" /> </form>
Code:// Initialize forms: function findClass() { var x=document.getElementsByTagName('form'); for(i=0;i<x.length;i++) { if(x[i].className.indexOf("ajax")>-1) { return ajaxInit(); } } } function ajaxInit(){ for (var i = 0; i < document.forms.length; i++){ var curForm = document.forms[i]; if (curForm.method == "post") { curForm.onsubmit = post_method; // Apply post_method to *every* form on the page } else if (curForm.method == "get") { curForm.onsubmit = get_method; } } } <form class="ajax" action="" method="post" id="{$ip}chk"> <input type="hidden" value="revSubmit.php" name="resultspage" id="resultspage" /> <input type="hidden" value="check" name="mode" /> <input type="hidden" value="{$ip}" name="ip" /> <input type="submit" value="Check" /> </form>
-
May 18, 2007, 06:18 #6
- Join Date
- Sep 2002
- Location
- Bournemouth, South UK
- Posts
- 1,551
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
I think it would be better to put it in your ajaxInit function, although I'm not sure where you want it, but something like this
Code:function ajaxInit(){ for (var i = 0; i < document.forms.length; i++){ if(document.forms[i].className.indexOf("ajax")>-1){ var curForm = document.forms[i]; if (curForm.method == "post") { curForm.onsubmit = post_method; // Apply post_method to *every* form on the page } else if (curForm.method == "get") { curForm.onsubmit = get_method; } } } }
LiveScript: Putting the "Live" Back into JavaScript
if live output_as_javascript else output_as_html end if
-
May 18, 2007, 09:32 #7
- Join Date
- Jan 2007
- Posts
- 14
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
but dont i need to somehow init the ajax function in the beginning? Should i keep this code in then?
Code:window.onload = ajaxInit;
-
May 18, 2007, 12:30 #8
- Join Date
- Jan 2007
- Posts
- 14
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
awesome, combination of your last post and the window.onload = ajaxInit; worked perfectly. You are the man! Can you PM me your paypal address so i can at least buy you a drink?
-
May 19, 2007, 02:16 #9
- Join Date
- Sep 2002
- Location
- Bournemouth, South UK
- Posts
- 1,551
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Not to worry dude, I only know the stuff because I've been taught it here
LiveScript: Putting the "Live" Back into JavaScript
if live output_as_javascript else output_as_html end if
Bookmarks