Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can document.on be changed by an ajax postback

Posted on 2014-10-13
2
Medium Priority
?
432 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 34

Accepted Solution

by:
Slick812 earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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)
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 …
Suggested Courses

782 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