Solved

Can document.on be changed by an ajax postback

Posted on 2014-10-13
2
396 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
ID: 40378907
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
ID: 40388829
AWESOME
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

919 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

19 Experts available now in Live!

Get 1:1 Help Now