Examples of dynamically adding options and createElement

  • 2020-03-30 01:27:41
  • OfStack

 
<!DOCTYPE html> 
<html> 
<head> 
<title>select.html</title> 

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
<meta http-equiv="description" content="this is my page"> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 

<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> 

</head> 

<body> 
<select id = "myCourse" onchange = "getCourse();"> 
<option value = "" selected>-- Please choose a course --</option> 
</select> 

<textarea id = "myares" rows="10" cols="30"></textarea> 
<script type="text/javascript" type = "text/javascript"> 
<!-- 
var last_select_num = 3;//Join query from database

//Dynamically add the first course
var myOption = document.createElement("option"); 
myOption.value = "java"; 
myOption.text = "java"; 
myCourse.add(myOption); 

//Dynamically add a second course,
myOption = document.createElement("option"); 
myOption.value = "oracle"; 
myOption.text = "oracle"; 
myCourse.add(myOption); 

//Dynamically add a third course,
myOption = document.createElement("option"); 
myOption.value = "javaEE"; 
myOption.text = "javaEE"; 
myCourse.add(myOption); 

function getCourse(){ 
myares.value += " You chose: "+myCourse.value +"rn"; 
} 
--> 
</script> 
</body> 
</html> 

 

Related articles: