ternary operator Demo

In this example below you will see how to do a ternary operator Demo with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by yuanyu, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright yuanyu ©

Technologies

  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>ternary operator Demo</title>
  
  
  
  
  
</head>

<body>

  
<label for="age">age:</label>
<input id="age" type="number"/>
<div id="mydiv"></div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/yuanyu/ternary-operator-demo-vpbGLb */
"use strict";
var myd = $("#mydiv");
var age = $("#age");
age.on('keyup', function(e){
  var age = Number(e.target.value);
  myd.text(age >= 18 ? "先生": age>= 12 ?"少年":"兒童");
});
age.on('change', function(e){
  var age = Number(e.target.value);
  myd.text(age >= 18 ? "先生": age>= 12 ?"少年":"兒童");
});

Comments