Solved

Can document.on be changed by an ajax postback

Posted on 2014-10-13
2
409 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

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 DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

751 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