Solved

Can document.on be changed by an ajax postback

Posted on 2014-10-13
2
387 Views
Last Modified: 2014-10-18
Hi

I was just  reading some past questions and want to make sure I understand.

Cn document.on be changed by an ajax postback e.g.

http://www.experts-exchange.com/Programming/Languages/Scripting/AJAX/Q_28442370.html

The underlying issue is that i dont know how to correctly handle the code if an ajax request returns a form that needs validating. I validate using jquery validation module and normally just put the validation code in doc.ready

This was discussed here but many other projects have happened since then so i wanted to be sure i had understood

http://www.experts-exchange.com/Programming/Languages/Scripting/AJAX/Q_28442370.html

thanks
0
Comment
Question by:andieje
2 Comments
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
Comment Utility
greetings  andieje, , you ask - "can document.on be changed by an ajax postback ", the short answer is YES, in javascript, all variables are re-definable , as you can change or redefine most any function, since in JS functions are really treated as variables, a "named" reference to a data set or operation chain.

in any function you can define it the first time -
   $(document.body).on('click', '#MyFormSubmitButton', function(){
   alert("first definition");
    });

and then in a separate function, you can change the "on" function to do another job -
function changeOn () {
   $(document.body).on('click', '#MyFormSubmitButton', function(){
   out = $('#namer').html+" has this new";
    });
}

= = = = = = when ever you get code BACK from an Ajax call to server, it executes a function (used to be called success() ) now it should be called .done, the new and more supported name for the ajax return. When the .done function is executed, you can apply the SAME code you used in the document ready -
    jQuery(document).ready(function(){

only place it INSIDE of the .done function -
ajax.done(function( received ) {// used to be ajax.success(
  $(document.body).on('click', '#MyFormSubmitButton', function(){
      out = $('#namer').html+" has this new";
      });
  });

Open in new window


you can place other functioning code there such as validation RE-definition code to change the validation to the new form using properties from the ajax return json

However, this as all code work, requires you to know what is needed and allowed in the second validation code set, and what is NOT allowed, It may not be the identical code that the FIRST validation code used.
0
 

Author Comment

by:andieje
Comment Utility
AWESOME
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now