user1423276
user1423276

Reputation: 65

How to make javascript prompt box cancel button to do nothing?

How can I make cancel button in javascript prompt box to do nothing like the cancel button on confirmation box instead of sending "null" values?

Or is it possible to remove the cancel button from the prompt box so there is only "ok" button left?

This is what I have tried but it still sends the null to value to my PHP file.

 function AskQuestion(data)
{
   var id = getUrlVars()["id"];
   console.log(data);
   if(data.key[0].status == "ok") {
      var reply = prompt(data.key[0].REPLY, "");
      var index = data.key[0].IND;
      if(reply != "" && reply !== null) {
      //do nothing
      }else{
      jQuery.ajax({ type: "POST",
                    url: serviceURL + "message.php",
                    data: 'id='+id+'&arvo='+reply+'&index='+index,
                    cache: false,
                    success: AskQuestion});
                    }
   } else {
     window.location = "page.html"
   }
}

Upvotes: 0

Views: 2520

Answers (3)

Supr
Supr

Reputation: 19022

You've got your test on the return value the wrong way around. As it is you // do nothing when the user enters text, and call ajax when they don't. Change

if(reply != "" && reply !== null) {
    // do nothing

to

if(reply == null || jQuery.trim(reply).length == 0) {
    // do nothing

Upvotes: 1

Chris Li
Chris Li

Reputation: 3725

You can just return to exit the function

Upvotes: -1

biziclop
biziclop

Reputation: 14596

A few ideas:

reply = jQuery.trim( prompt(...) || "" );
if( reply ){
  jQuery.ajax(...)
}

Upvotes: 1

Related Questions