Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display massage after pjax success by clicking on specific input only

I have a form which submits when any change is made on any of the radio input's.

Inside the form I have two different radio groups. I am trying to output a message on my #message on pjax:start when any of the radios from group2 only is triggered.

<form id="myForm">
    <input type="radio" name="group1" val="ga_1_1">
    <input type="radio" name="group1" val="ga_1_2">

    <input type="radio" name="group2" val="ga_2_1">
    <input type="radio" name="group2" val="ga_2_2">
</form>

<span id="message"></span>

Here is my simplyfied approach:

$('input[name="group2"]').on('change', function(){
   $(document).on('pjax:start', function() {
     $('#message').text( 'Loading...' );
   });

   $(document).on('ready pjax:success', function() {
     $('#message').text( 'Loaded' );
   });
});

Problem is that the message triggers even if I click on radio group1.

like image 791
Dom Avatar asked Dec 02 '16 17:12

Dom


People also ask

How to display success message when Submit button is clicked?

When the Submit Button is clicked, first the record is inserted into the database and then using ClientScript RegisterStartupScript function, the success message is displayed in JavaScript alert message box. script – The JavaScript code that will display the Alert message box.

How to display a message box on button click in HTML?

So, let’s get started with HTML coding to display a message box on a button click. First of all, create a button element that will be used to reveal the message box. You can also create a div element virtual button to display a message box in HTML. After that, create HTML for message box and background dim overlay as follows:

What is the message type of'display success message'?

//Display success message. string message = "Your details have been saved successfully."; 'Insert record here. 'Display success message. Dim message As String = "Your details have been saved successfully."

How to display a message in JavaScript?

Welcome to a quick tutorial on how to display messages in Javascript. Just started with Javascript and wondering how to display a message? The common ways to show messages in HTML and Javascript are: Alert, prompt, confirm. Output the message to the developer’s console. Dialog box (popup box).


1 Answers

You've the right selector no need for change it, you could use $.pjax callbacks directly :

$('input[name="group2"]').on('change', function(){
    $.pjax({
        url: '/url',
        container: '#container',
        beforeSend: function(){
            $('#message').text( 'Loading...' );
        },
        complete: function(){
            $('#message').text( 'Loaded' );
        }
    });
});

Or also you could put out the callbacks and use send & complete instead as are the best if you're in loading purpose as described in the Official Documentation :

pjax:send & pjax:complete are a good pair of events to use if you are implementing a loading indicator

$(function(){
    $('input[name="group2"]').on('change', function(){
       $.pjax({url: "/url", container: '#container'})
    });

    $(document).on('pjax:send', function() {
        $('#message').text( 'Loading...' );
    })
    $(document).on('pjax:complete', function() {
        $('#message').text( 'Loaded' );
    })
})

NOTE : No need for the ready callback since it will init your #message with Loaded when the page is loaded before any $.pjax request.

Take a look to best way to implement an overlay with pjax.

Hope this helps.

like image 99
Zakaria Acharki Avatar answered Sep 30 '22 06:09

Zakaria Acharki