Force immediate display of a value

I am testing for a value in a displayed field and if the value goes over a certain number I want to issue and ‘alert’ and then force the field value back to the limiting value.
I update the field and then issue the ‘alert’ but the field display doesn’t update until after the ‘alert’ is displayed. I want the value displayed before the ‘alert’ . Whatever I try the value doesn’t display until after the ‘alert’. How do I get around this?
(jQuery)

$( '#some_field' ).val( '444' );
alert( '......................' );

Well it is a bit of a bummer that it works this way but it is part of the reason not to really use alert/prompt/confirm if you can all help it. Alerts are controlled by the browser system independent of the JS running in the background. As for your particular situation, the answer is pretty much use setTimeout. Here is a stackoverflow thread that talks about this and gives solutions.

I hope this helps. :slight_smile:

1 Like

I was beaten to the punch by @Martyr2 :slight_smile: but I was going to suggest the same thing.

$("#some_field").val("444");
var response = "You have reached the maximum";
setTimeout(function () {
  alert(response);
}, 1);

It would be better to write your own alert box and have total control and customisation with html and css,

Thanks to both of you. I didn’t know the origin of the timing but I had thought of the setTimeout.

Do you guys EVER put messages out in dialog form and, if so, how do you handle it? PaulOB’s suggestion of rolling your own seems unnecessary since I have to imagine that there has been SOME occasion when many in the JS community have thought it would be nice to be able to pop a message on the screen and have, therefore developed a standard way of doing it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.