Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript focus on div while showing alert box

What I am trying to do is create a sequence of alert pop-ups that will be used as a tutorial for learning a websites' functionalities. The pop-ups will appear one after another explaining what each "div" of the website does. When a pop-up appears I want the referenced "div" to be focused ( brought to foreground ) so that it would be more visible.

The code looks like this and obviously it does not work:

var someDiv = document.getElementById('someID');
someDiv.focus();

alert("Message explaining functionality");

What am I doing wrong?

like image 213
kplatis Avatar asked Mar 17 '17 20:03

kplatis


People also ask

Can we apply focus on Div?

Yes - this is possible. In order to do it, you need to assign a tabindex...

Can we customize alert box in JavaScript?

The standard alert box in JavaScript does not provide the option to apply CSS. To style your alert box, you need to create a custom one first. The custom alert box will be created using jQuery and styles will be applied to CSS.

What happens when JavaScript runs the alert () function?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.

Which is the standard way to show alert in JavaScript?

The alert() method in JavaScript displays an alert box with a specified message and an OK button. It is often used to make sure that information comes through to the user. The alert box takes the focus away from the current window and forces the browser to read the message.


2 Answers

To make a div focusable you've to use tabindex :

<div id='someID' tabindex='1'>MY DIV TEST</div>

Also try to give it a while to focus using setTimeout.

NOTE : I suggest really the use of another "flexible" modal plugin than the alert().

Hope this helps.

var someDiv = document.getElementById('someID');
someDiv.focus();

setTimeout(function(){
  alert("Message explaining functionality");
},10)
<div id='someID' tabindex='1'>MY DIV TEST</div>
like image 91
Zakaria Acharki Avatar answered Oct 04 '22 23:10

Zakaria Acharki


You could try with this to make your tutorial. I already made once something similar. Check project here.

$(document).ready(function() {

      // Initialize the plugin
      $('#my_popup').popup();

    });
    
<button class="my_popup_open">Tutorial</button>
<!-- Content of popup -->
<div id="my_popup">
I'm Tutorial
<button class="my_popup_close">Close</button></div>

  <!-- Include jQuery -->
  <script src="https://code.jquery.com/jquery-1.8.2.min.js"></script>

  <!-- Include jQuery Popup Overlay -->
  <script src="https://cdn.rawgit.com/vast-engineering/jquery-popup-overlay/1.7.13/jquery.popupoverlay.js"></script>
like image 28
Danilo Ivanovic Avatar answered Oct 05 '22 01:10

Danilo Ivanovic