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?
Yes - this is possible. In order to do it, you need to assign a tabindex...
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.
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.
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With