-1

I want to replace the default window for messages in ajax with a div, for example I want to replace this code: alert(data.status + ": " + data.message); with a div.

This is my ajax code:

//Start ajax code
                if(!url){
                   url = jQuery('#product_addtocart_form').attr('action');
                }
                url = url.replace("checkout/cart","ajax/index"); 
                var data = jQuery('#product_addtocart_form').serialize();
                data += '&isAjax=1';   
                jQuery('#ajax_loader').show();
                try {
                    jQuery.ajax({
                          url: url,
                          dataType: 'json',
                          type : 'post',
                          data: data,
                          success: function(data){
jQuery('.messages').remove();
                                jQuery('#ajax_loader').hide();
                                //alert(data.status + ": " + data.message);



                                if(jQuery('.header .links')){
                                    jQuery('.header .links').replaceWith(data.toplink);
                                }

if(jQuery('.mini-cart')){
   jQuery('.mini-cart').replaceWith(data.sidebar);
   var status = data.status;

   showMessage(status.toLowerCase(), data.message);
}

// Create below function in your jquery
function showMessage(txt, type) {
  var html = '<div id="messages_product_view"><ul class="messages"><ul class="messages"><li class="'+type+'-msg"><ul><li>' + txt + '</li></ul></li></ul></div>';
  jQuery('.messages').update(html);
}



                          }
                    });
                } catch (e) {
                }
//End ajax code

In this code now I have this error:

TypeError: jQuery(...).update is not a function
jQuery('.messages').update(html);
Robert
  • 812
  • 3
  • 18
  • 47
  • 2
    What is the update function, try to use .html(). jQuery('.messages').html(html); – Mohit Tanwani Sep 27 '16 at 09:10
  • What's update supposed to do? This is not a standard jquery method, so why would you expect it to exist? – Liam Sep 27 '16 at 09:24
  • Hi, is ok to use html instead update but
    is blank I don't have any data
    – Robert Sep 27 '16 at 09:24
  • Possible duplicate of [How to replace innerHTML of a div using jQuery?](http://stackoverflow.com/questions/1309452/how-to-replace-innerhtml-of-a-div-using-jquery) – Liam Sep 27 '16 at 09:25
  • Is not a duplicate, please read again my post, is not have any related thing – Robert Sep 27 '16 at 09:26

2 Answers2

1

to set text/html to an element you can use like this...

var html = '<div id="messages_product_view"><ul class="messages"><ul class="messages"><li class="'+type+'-msg"><ul><li>' + txt + '</li></ul></li></ul></div>';
jQuery('.messages').html(html);

use .html() function instead of .update()

Skalbhile
  • 156
  • 13
1

try

$('#messages_product_view').append(html);
beerwin
  • 9,813
  • 6
  • 42
  • 57
FishMan
  • 127
  • 2
  • 11