nuet
nuet

Reputation: 219

how to disable text in textarea when prompting

I have this case in a switch which puts preprint text in a textarea when press OK on prompt. When I press CANCEL, there appears this text in textarea:

<img src="null" />

This is the case:

  case 'image':
           var urlOpts = {};
           var thisOpts = $.extend(htmlOpts, {
              
              closetype: 'short'
           });
           NewURL = prompt("URL van afbeelding:",'http://');
           /urlOpts.src = NewURL;
           
           $(TextArea).insertRoundTag('img',thisOpts,urlOpts);
           break;

How can I disable that text when pressing CANCEL in prompt? Textarea should be left blank.

Upvotes: 1

Views: 62

Answers (2)

Alex Char
Alex Char

Reputation: 33218

Javascript prompt returns a string or null if user press cancel. So you can check if value is different from null:

NewURL = prompt("URL van afbeelding:", 'http://');
if (NewURL != null) {
  urlOpts.src = NewURL;

  $(TextArea).insertRoundTag('img', thisOpts, urlOpts);
}

Upvotes: 1

Markai
Markai

Reputation: 2098

You can use an if statement. Because the prompt returns null if you press cancel, you can just check NewURL against null:

case 'image':
    var urlOpts = {};
    var thisOpts = $.extend(htmlOpts, {

        closetype: 'short'
    });
    NewURL = prompt("URL van afbeelding:",'http://');
    if(NewURL != null){
        urlOpts.src = NewURL;

        $(TextArea).insertRoundTag('img',thisOpts,urlOpts);
    }
    break;

Upvotes: 1

Related Questions