Skip to content
Advertisement

How to use in my case if option selected (do something)

This is my HTML and JavaScript:

var seq = 0;
// var d = new Date();

var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
newdate = day + "/" + month + "/" + year;

function myFunction() {
  // Increment the value
  seq += 1

  // The string value that will be displayed
  var value = '';

  // If sequence is less than 10, prefix with 000
  if (seq < 10) {
    value = '00' + seq + "-" + newdate;
  }
  // If sequence is less than 100, prefix with 00
  else if (seq < 100) {
    value = '0' + seq + "-" + newdate;
  }
  // If sequence is less than 1000, prefix with 0
  else if (seq < 1000) {
    value = '' + seq + "-" + newdate;;
  }
  // Otherwise, just use the value directly
  else {
    value = seq;
  }

  // Display the formatted value (ie prefixed with 0's)
  document.getElementById("demo").value = value;

}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Ramndom Number Auto - generate based on current date</title>
  <script src="randomdate.js"></script>
</head>

<body>

  <input type="checkbox" name="alegeT" id="alegeT"><label for="alegeT">Tricouri</label><input type="checkbox" name="alegeP" id="alegeP"><label for="alegeP">Prod</label>

  <h1>This is a simple application that helps you to generate a random number based on current date</h1>

  <button onclick="myFunction()">Try it</button>
  <input id="demo" type="text">


</body>

</html>

so as you can see in the HTML I have 2 check boxes so when I choose the checkbox “tricouri” I want to get generate a number like this:

value = “T” + ’00’ + seq + “-” + newdate;

and when I choose “Prod” I want to get a number like this:

value = “P” + ’00’ + seq + “-” + newdate;

Advertisement

Answer

I recommend you start with learning HTML first before you start diving deep in JavaScript. I made this rather messy example for you to give you an idea.

  • I think you want to use radiobuttons instead of checkboxes.

// var d = new Date();

var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var newdate = day + "/" + month + "/" + year;
var seq = 0; // initiate and declare seq

function myFunction(event) {
  //prevent default
  event.preventDefault();
  // Increment the value
  seq += 1;
  //fetch the first prefix
  let prefix = event.currentTarget.form.querySelector(
    "[data-type='selection']:checked"
  ).value;

  //adds some 0's before seq and only fetch the needed ones 
  let zeros = `0000000${seq}`.substr(-4);
  //glue it up
  let value = `${prefix}-${zeros}-${newdate}`;

  // Display the formatted value (ie prefixed with 0's)
  event.currentTarget.form.querySelector("#demo").value = value;
}
<!-- inputs belong in a form or iputs are given the form attribute -->
<form>
  <input type="radio" name="choice" id="alegeT" data-type="selection" checked="checked" value="T">
  <label for="alegeT">Tricouri</label>
  <input type="radio" name="choice" id="alegeP" data-type="selection" value="P">
  <label for="alegeP">Prod</label>

  <h1>This is a simple application that helps you to generate a random number based on current date</h1>

  <button onclick="myFunction(event)">Try it</button>
  <input id="demo" type="text">
</form>
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement