How do i stay on the same page after clicking ok on the alert box using javascript? | XM Community
Solved

How do i stay on the same page after clicking ok on the alert box using javascript?

  • 9 June 2021
  • 2 replies
  • 571 views

Userlevel 5
Badge +11
  • QPN Level 5 ●●●●●
  • 78 replies

Here is the code that I'm applying, it does give an alert box but as soon as I click on ok rather than letting me edit the mistake it takes me to the next page. How do I stop redirection?
Qualtrics.SurveyEngine.addOnReady(function()
{
var that = this;
jQuery("#NextButton").hide();
jQuery("#NextButton").after('');
var txtEntry = jQuery("input[id*='QR~QID'][type='text']");

jQuery("#CustomNextButton").click(function() {
var v1 = parseInt(txtEntry.eq(0).val());
var v2 = parseInt(txtEntry.eq(1).val());
var v3 = parseInt(txtEntry.eq(2).val());
var v4 = parseInt(txtEntry.eq(3).val());
var v5 = parseInt(txtEntry.eq(4).val());
var v6 = parseInt(txtEntry.eq(5).val()); 
var v7 = parseInt(txtEntry.eq(6).val());
var v8 = parseInt(txtEntry.eq(7).val());
var v9 = parseInt(txtEntry.eq(8).val());
var vtot1 = v2 + v3 + v4 + v5 - v6;
var vtot2= v1 - vtot1;
if (vtot2 != v7)
{ alert("Please enter the correct figures, Balance at 31 december = Balance at 1 January - Leavers during the year (resignation, retrenchments, dismissed, retirements + Number of new joiners) ");} 
else { that.clickNextButton();}
var per_staff= v8;
var temp_staff= v9;
var total_temp_per_staff = per_staff + temp_staff;
if (total_temp_per_staff != v7)
{ alert("Please enter the correct figures, Balance at 31 december = Permanent Staff + Temporary Staff ");
}
else { that.clickNextButton();}
});
});

icon

Best answer by Eumetis 11 June 2021, 16:11

View original

2 replies

Badge +8

Just a minor change in code, add return after first alert, here's the updated code.
Qualtrics.SurveyEngine.addOnReady(function () {
    var that = this;


    jQuery("#NextButton").hide();
    jQuery("#NextButton").after('');


    var txtEntry = jQuery("input[id*='QR~QID'][type='text']");


    jQuery("#CustomNextButton").click(function () {


        var v1 = parseInt(txtEntry.eq(0).val());


        var v2 = parseInt(txtEntry.eq(1).val());


        var v3 = parseInt(txtEntry.eq(2).val());


        var v4 = parseInt(txtEntry.eq(3).val());


        var v5 = parseInt(txtEntry.eq(4).val());


        var v6 = parseInt(txtEntry.eq(5).val());


        var v7 = parseInt(txtEntry.eq(6).val());


        var v8 = parseInt(txtEntry.eq(7).val());


        var v9 = parseInt(txtEntry.eq(8).val());


        var vtot1 = v2 + v3 + v4 + v5 - v6;


        var vtot2 = v1 - vtot1;


        if (vtot2 != v7) {
            alert("Please enter the correct figures, Balance at 31 december = Balance at 1 January - Leavers during the year (resignation, retrenchments, dismissed, retirements + Number of new joiners) ");
            return;
        } else {
            that.clickNextButton();
        }


        var per_staff = v8;


        var temp_staff = v9;


        var total_temp_per_staff = per_staff + temp_staff;


        if (total_temp_per_staff != v7) {
            alert("Please enter the correct figures, Balance at 31 december = Permanent Staff + Temporary Staff ");
        } else {
            that.clickNextButton();
        }


    });


});

Userlevel 5
Badge +11

Thank you for your response🙂. I had tried this before and it hadn't worked. So I put all the ifs inside a main if condition and that worked
if (vtot2 != v7 || total_temp_per_staff != v7 || total != v7)

if(vtot2 != v7)
  {alert("Please enter the correct figures, Balance at 31 december = Balance at 1 January - Leavers during the year (resignation, retrenchments, dismissed, retirements + Number of new joiners) ");} 

if(total_temp_per_staff != v7)
 {alert("Please enter the correct figures, Balance at 31 december = Permanent Staff + Temporary Staff ");}

if( total != v7)
{alert("Please enter the correct figures, Balance at 31 december = No of male employees + No of female employees ");}
}

else { that.clickNextButton();}

Leave a Reply