Add new option to select javascript
Here is the updated code with a new option to select JavaScript:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Options</title>
</head>
<body>
<h1>JavaScript Options</h1>
<form>
<label for="javascript-option">Select a JavaScript option:</label>
<select id="javascript-option" name="javascript-option">
<option value="none">None</option>
<option value="alert">Alert</option>
<option value="confirm">Confirm</option>
<option value="prompt">Prompt</option>
<option value="javascript">Run JavaScript code</option>
</select>
<button type="submit">Submit</button>
</form>
<script>
const form = document.querySelector('form');
const optionSelect = document.querySelector('#javascript-option');
form.addEventListener('submit', (e) => {
e.preventDefault();
const selectedOption = optionSelect.value;
switch (selectedOption) {
case 'alert':
alert('You selected the alert option!');
break;
case 'confirm':
const result = confirm('Are you sure?');
if (result) {
alert('You confirmed!');
} else {
alert('You cancelled!');
}
break;
case 'prompt':
const input = prompt('Enter your name:');
if (input) {
alert(`Hello, ${input}!`);
} else {
alert('You cancelled!');
}
break;
case 'javascript':
const code = prompt('Enter your JavaScript code:');
if (code) {
try {
eval(code);
alert('Your code executed successfully!');
} catch (error) {
alert(`Error: ${error.message}`);
}
} else {
alert('You cancelled!');
}
break;
default:
alert('You selected none!');
}
});
</script>
</body>
</html>
In this updated code, I added a new option to the select menu with the value "javascript". When this option is selected and the form is submitted, the script will prompt the user to enter JavaScript code and then execute it using the eval()
function. Note that this is a basic example and you should be careful when executing user-input code to avoid security vulnerabilities.