Ask for user input using alert box

I want the user to input a piece of text in an alert box.

HTML:

<form>
    <select class="favoritefood">
        <optgroup label="Dairy products">
            <option>Cheese</option>
            <option>Egg</option>
            <option id="newDairy">NEW</option>
        </optgroup>
        <optgroup label="Vegetables">
            <option>Cabbage</option>
            <option>Lettuce</option>
            <option>Beans</option>
            <option>Onions</option>
            <option>Courgettes</option>
            <option id="newVeg">NEW</option>
        </optgroup>
    </select>
</form>

JS:

$('.favoritefood').change(function () {
    var id = $(this).find('option:selected').attr('id');
    if (id == 'newDairy') {
        alert("input");
    };
});

I know it is possible to have an input inside the alert box, but how?

Answers:

Answer

That would be the prompt box

var result = prompt('type something');

FIDDLE

Answer

You can also specify a default value for the prompt using

var value = prompt('Enter Value', 'Default Value');

Also note: "Cancel" from the prompt will return null.

Try a demo here: http://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.