Write a JavaScript program to count and display the items of a dropdown list, in an alert window
<!-- //Write a JavaScript program to count and display the items of a dropdown list, in an alert window -->
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<style type="text/css">
body {
margin: 30px;
}
#confirm {
display: none;
background-color: #F3F5F6;
color: #000000;
border: 1px solid #aaa;
position: fixed;
width: 300px;
height: 100px;
left: 40%;
top: 40%;
box-sizing: border-box;
text-align: center;
}
#confirm button {
background-color: #FFFFFF;
display: inline-block;
border-radius: 12px;
border: 4px solid #aaa;
padding: 5px;
text-align: center;
width: 60px;
cursor: pointer;
}
#confirm .message {
text-align: left;
}
</style>
<title>Count and display items of a dropdown list - w3resource</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="confirm">
<div class="message" id="idmsg">This is a warning message.</div><br>
<button class="yes">OK</button>
</div>
<input type="button" value="Click Me" onclick="functionAlert();" />
<form>
Select your favorite Color :
<select id="mySelect">
<option>Red</option>
<option>Green</option>
<option>Blue</option>
<option>White</option>
</select>
<input type="button" onclick="getOptions()" value="Count and Output all items">
</form>
<script>
function functionAlert(msg, myYes) {
var confirmBox = $("#confirm");
confirmBox.find(".message").text(msg);
confirmBox.find(".yes").unbind().click(function () {
confirmBox.hide();
});
confirmBox.find(".yes").click(myYes);
let idmsg =document.getElementById('idmsg').style.textAlign = "center";
confirmBox.show();
}
function getOptions() {
let mySelect = document.getElementById('mySelect');
let length = document.getElementsByName('option').length;
alert(mySelect.length);
functionAlert(mySelect.length);
}
</script>
</body>
</html>
Comments
Post a Comment