Testing Buttons with JS

How to customize the JavaScript prompt?

I used buttons instead of input fields for the options since making the user input their choices gets pretty annoying. The object is a map of path id (e.g. ‘intro’, ‘choose_weapon’) to a scenario object.

Custom / Styled prompt window?

I’m trying to make a ‘Choose your Adventure’ game, and I want to know if it’s possible to make a styled/custom ‘Prompt’ window, and if it can be not opened up as a ‘prompt’ window, but have the prompt and user input in a selected HTML box? This is what I mean. If my HTML has

HTML

CSS

and JavaScript (I probably will mess up the code)

JavaScript

var prompt = document.getElementByClassName("prompt"); var choice = prompt("What is your choice? CHOICE1, CHOICE2, or CHOICE3?").toUpperCase(); prompt.innerHTML = choice; 

and I hope to get something like the prompt not showing up a dialogue window but instead putting the prompt text into the textarea, and the user put in their choice with the input, then submit it by the submit button. How could I get it so that the prompt window instead outputs the question/text to the textarea , and the user puts in their answer via the input text field, and submitting it via the input submit button, and it works like normal. Is this even possible? If not, is it at least possible to style the prompt dialogue box itself? Here’s my code so far.

@import url(//fonts.googleapis.com/css?family=Permanent+Marker);html, body < background: #000; margin: 0; padding: 0; >#wrap < width: 760px; margin-left: auto; margin-right: auto; >.container < position: relative; top: 50px; margin-left: auto; margin-right: auto; width: 570px; height: 350px; border: 6px ridge orange; padding: 0; >.container img < position: absolute; bottom: 0px; width: 570px; height: 350px; z-index: -1; >p.intro < color: black; text-shadow: -1px -1px 0 #FFF, 1px -1px 0 #FFF, -1px 1px 0 #FFF, 1px 1px 0 #FFF; >h2.header < text-shadow: -1px -1px 0 #FFA500, 1px -1px 0 #FFA500, -1px 1px 0 #FFA500, 1px 1px 0 #FFA500; >.box < float: left; min-width: 567px; min-height: 350px; >.box h2 < font-family: 'Permanent Marker', cursive; font-size: 200%; text-align: center; >.box p < font-family: 'Permanent Marker', arial; text-align: center; >.box a < position: absolute; left: 165px; display: inline-block; border: 3px groove #000; border-radius: 5px; background: red; margin-left: auto; margin-right: auto; width: 225px; height: 75px; font-family: 'Permanent Marker', cursive; color: #FFA500; text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000; text-align: center; >.battles img

   


BEGIN!

Here is an example using jQuery. jQuery is useful for manipulating DOM elements. Instead of selecting an object by doing:

document.getElementById('someid'); 

You would select the element as you would in CSS:

Читайте также:  Быстрый переход внутри документа

In my adventure game example, I used a JSON object to contain my story and its paths. The story object is a map of path id (e.g. ‘intro’, ‘choose_weapon’) to a scenario object. This helps to organize your story.

I used buttons instead of input fields for the options since making the user input their choices gets pretty annoying.

// Contains the story and paths var story = < intro: < prompt: 'It is 12am and you are starving. It\'s too late to order delivery. You know what that means.', options: [< name: 'Fight', path: 'choose_weapon' >, < name: 'Starve', path: 'die_starve' >] >, choose_weapon: < prompt: 'Choose your weapon!', options: [< name: 'Knife', path: 'die_cut' >, < name: 'Toaster', path: 'toast' >] >, toast: < prompt: 'You toast some bread. What do you do next?', options: [< name: 'Eat it!', path: 'eat' >, < name: 'Slather on some peanut butter!', path: 'peanut_butter' >] >, peanut_butter: < prompt: 'There is now peanut butter on your bread. Excellent choice. What do you do next?', options: [< name: 'Eat it!', path: 'eat' >, < name: 'Throw it away', path: 'die_starve' >] >, eat: < prompt: 'It was delicious! You are no longer hungry.', options: [< name: 'Start Again', path: 'intro' >] >, die_cut: < prompt: 'You accidentally cut yourself and bleed to death.', options: [< name: 'Start Again', path: 'intro' >] >, die_starve: < prompt: 'You have died of hunger!', options: [< name: 'Start Again', path: 'intro' >] > >/** * Chosen option is an object with properties */ function display_scenario(chosen_option) < var option_name = chosen_option.name; var option_path = chosen_option.path; var scenario = story[option_path]; // Clear the #prompt div and the #options div $('#prompt').empty(); $('#options').empty(); // Create a 

to display what the user has chosen if option_name is not null and append it to the #prompt if (option_name) < $('

').html('You have chosen ' + option_name + '').appendTo('#prompt'); > // Append the scenario's prompt $('

').html(scenario.prompt).appendTo('#prompt'); // Append the options into the #options // We want to loop through all the options and create buttons for each one. A regular for-loop would not suffice because adding a button is not asynchronous. We will create an asynchronous loop by using recursion function add_option_button(index) < if (index === scenario.options.length) < // Base case return; >var option = scenario.options[index]; // Create a for this option and append it to the #options $('') .html(option.name) .click(function(e) < // This is an onclick handler function. It decides what to do after the user has clicked on the button. // First, prevent any default thing that the button is going to do, since we're specifying our own action for the button e.preventDefault(); // We'll want to call display_scenario() with this option display_scenario(option); >) .appendTo('#options'); // Add the next option button add_option_button(index + 1); > add_option_button(0); >// This function waits until the document is ready $(document).ready(function() < // Start the story display_scenario(< name: null, path: 'intro' >); >);

@import url(//fonts.googleapis.com/css?family=Open+Sans:400,700);* < margin: 0px; padding: 0px; color: #32363F; font: 18px 'Open Sans', sans-serif; border: none; outline: none; box-sizing: border-box; >html < display: table; width: 100%; height: 100%; >body < display: table-cell; background: #32363F; vertical-align: middle; >#wrapper < margin: 40px; background: #D6C2A3; width: calc(100% - 80px); >h1 < display: block; padding: 20px 20px 12px; font: 700 36px 'Open Sans', sans-serif; background: #E84949; color: #FAFAFA; text-transform: uppercase; >#prompt < padding: 20px; >#prompt p < padding-bottom: 8px; >#prompt p b < font-weight: 700; >#options < display: flex; padding: 0px 20px 28px; text-align: center; >#options button < margin: 0px 8px; padding: 8px 20px; background: #C2AE8F; width: 100%; cursor: pointer; >#options button:hover, #options button:active

Customize the Prompt in JavaScript

The default prompt, alert, and confirm functions in JavaScript are not customizable. Here’s Duration: 5:29

Custom Prompt Box Programming JavaScript CSS HTML Tutorial

Lesson Code: http://www.developphp.com/video/JavaScript/Custom-Prompt-Box-Programming Duration: 10:27

Custom Prompt Dialog in JS

Learn to create your custom prompt dialog in JS, with HTML for layout & CSS for appearance.
Duration: 14:45

Design javascript’s default prompt box

i want to get a value from user with use of JavaScript’s prompt. i write this code. but it is too simple. how to design it with css or any other?

The only way around it is to create your own prompt using html css and javascript. see example below that you could expand on.

/* show Prompt plugin */ var showPrompt = (function() < var promptEl = document.querySelector('.prompt'), _cb = null; var prompt = < el: promptEl, form: promptEl.querySelector('.prompt__form'), text: promptEl.querySelector('.prompt__text'), input: promptEl.querySelector('.prompt__input'), submit: promptEl.querySelector('.prompt__submit') >prompt.form.addEventListener('submit', hide, false); function show( text, cb ) < prompt.el.classList.add('prompt--show'); prompt.text.innerHTML = text; _cb = cb; >function hide( e ) < e.preventDefault(); prompt.el.classList.remove('prompt--show'); _cb.call( prompt, prompt.input.value ); >return show; >)();// show the prompt showPrompt('Enter Your Text Here', function( answer )< console.log( 'prompt submitted', answer ); >);
* < box-sizing: border-box; >.prompt < /* make the backgronud full screen */ position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(40,40,40,.37); /* make it show on top of all other elements */ z-index: 999; /* hide the prompt by default */ visibility: hidden; opacity: 0; transition: .6s opacity, .6s visibility; >.prompt__form < /* center the prompt window */ position: fixed; top: 50%; left: 50%; transform: translate( -50%, -50% ); width: 400px; padding: 1em; background: #e5e5e5; >.prompt--show < visibility: visible; opacity: 1; >.prompt__input < width: 80%; float: left; >.prompt__submit

Window Object, The window object represents an open window in a browser. If a document contain frames (

How to make custom prompt boxes within functions in JavaScript? [duplicate]

I started coding a button program about a week ago, and have made quite a bit of progress. I’m new to HTML, CSS and JS, but seem to be learning quite quickly. The thing I want to do next, is create a custom prompt + alert box for my code, but I can’t figure out how to do it within functions. This is my code:

        









As you can see, I’ve got a lot of prompts and alerts, but they’re default and I want to decorate them. Please advise me on how to do this (ideally with an example)

You can’t edit the css properties of alert box, or any other (prompt/confirm/..) box

That is because alert() itself is a javascript object and not a HTML entity (tag). What you however can do, is to create a hidden div that would imitate alert box and upon EventHandler you would change it’s property as visible, which would act like an alert box.

It is an immitation, but that is closest you can get to custom styled alertbox. Often times it’s actually a bit more practical than an actual alert()

Here is a (rather ugly) example of it in action:

var btn = document.getElementById('alertbutton'); var alertmsg = document.getElementById('alert'); var okbtn = document.getElementById('okbtn');btn.addEventListener('click', function()< alertmsg.classList.remove('hidden'); //execute some other code if necessary okbtn.addEventListener('click',function()< alertmsg.classList.add('hidden'); // also some other code if necessary >); >);
#lorem < width: 320px; height: 320px; overflow: hidden; background-color: #cecece; border: 4px solid black; word-wrap: break-all; >button < position: relative; top: -220px; left: 350px; height: 30px; >#alert < position: relative; width: 60%; left: 50%; bottom: 40vw; transform: translateX(-50%); border: 4px solid red; padding: 20px; background-color: #FF9999; >.OK < position: absolute; display: block; float: right; right: 30px; bottom: 30px; height: 40px; width: 60px; >.hidden
 

Customize the Prompt in JavaScript, The default prompt, alert, and confirm functions in JavaScript are not customizable. Here’s Duration: 5:29

Источник

Custom and fancy JavaScript prompt alerts

The prompt keyword is followed by the text which is shown as a message and text in the textbox which is also visible to take the user input.

The above prompt JavaScript alert is quite simple that takes a browser’s default style. In many scenarios, this is desired to match the prompt dialogue design to the theme of the website.

I will show you creating simple prompt alerts as well as creating beautiful JavaScript plug-ins based alerts in this tutorial. You can change the look and feel of these alerts quite easily.

Let me start with a basic prompt alert by using the prompt keyword.

A basic prompt alert example

In this example, a simple prompt alert is created where a user is asked to enter the email address. If user enters the email and presses Ok, a simple alert will be shown with entered email address:

JavaScript prompt simple

See online demo and code

Instead of showing the alert, you can perform some action like saving the email address or retrieving information from the database based at that email etc.

Following JS code is used to create that prompt alert:

The promptalert JS function is called at the click event of the button.

A demo to create fancy prompt alert by using alertify.js plug-in

As mentioned earlier, you can create fancy and beautiful looking prompt alerts by using third party plug-ins to match with the design of your website. One of the plug-ins that I am going to show you is alertify.js.

First, have a look at an example of this plug-in which is followed by how to set up and customizing its look and feel.

JavaScript prompt alertify

See online demo and code

After including the dependency files in the section, following code is used in the part just above the tag:

You can see, the alert is initiated by using alertify.prompt which is followed by text to be displayed. If a user presses the OK button it will display a simple alert by using alertify.alert.

Setting up alertify.js plug-in

The plug-in requires a few dependency files in the section:

You may get these from the GitHub website. Or download by viewing the “view source” in the demo page by opening these files and save it to your system

Among those files, alertify.default.css is the one that deals with the look of alert. If you want to change the look according to the theme of your website, then either modify this file or use your own with similar classes. See a demo below, how you can do it.

A demo to change the JavaScript based prompt alert (alertify.js)

For this demo, I am simply copying the alertify.default.css and renaming it to alertify.custom.css. You also need to change the name in the head section as well.

See a demo where I changed a few CSS properties to modify the look of the prompt dialog.

JavaScript prompt alertify custom

See online demo and code

You can see, the background color of the main area in the alert is changed. Also, the button’s font-size and font-family are also changed.

To accomplish that, open the alertify.custom.css file. The .alertify class specifies the background-color and font related properties along with border-radius.

Similarly, you may change the Ok and Cancel button properties by modifying its class properties.

You may also change the text of OK or Cancel buttons to Yes/No or as per need by modifying the alertify.min.js or alertify.js file (whichever you include in your project).

A demo of prompt JavaScript alert by using SweetAlert plug-in

There is another nice plug-in to create different types of alerts including prompt which is also based at JavaScript.

In this demo, I will show you how you can use this to create a prompt alert by using SweetAlert plug-in.

JavaScript prompt sweetalert

See online demo and code

Following code is used in the section (you can see complete code in the demo page):

Источник

Оцените статью