Why can't I modify DOM before calling prompt function?

Viewed 21

I was trying to change a color before firing the prompt, but I realized I could not.

I am wondering, why does the color blue only happen if I press "cancel" after prompt?

What's the idea behind this? Did they think it's not OK to allow such behavior, or am I missing something?

$('button').click(() => {
    $('#ccs').css('background', 'blue'); // why isn't it becoming blue?
    let v = prompt('At this point I expected color to be blue', 'If you press cancel, it will become blue');
    
    if (v === null) {
        return; // in here color changes to blue. Why is this fired before line 2?
    }
    
    $('#ccs').css('background', 'red');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="ccs" style="background: red">block</div>
<br><br>
<button>Show prompt</button>

0 Answers
Related