Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
How do I make this line's width randomly grow?

I am trying to learn Javascript and integrate it into a webpage however when trying to use a JavaScript random function that changes the size of a line on click nothing happens?

function randomnumber(min, max) {
    var rn = Math.random() * (max - min) + min;
    document.getElementById("rain").style.width = rn;
}
body {
  background:#000000; 
}

.line {
  height: 1px;
  width: 40px;
  background: white;
  transform: rotate(45deg);
}
<div id="rain" onclick="randomnumber(1px,100px)">
    <div class="size">
        <div class="line"></div>
    </div>
</div>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The problem is you consider the 1px and 100px to be number, but the type of 1px and 100px is string. Also, you can't use 1px and 100px to do Math.

Also, another problem make your code won't work is your are trying to assign the width to parent element of line (the rain) and since rain doesn't have any style, it won't work as well. You should directly apply style for line

function randomnumber(min, max) {
  var rn = Math.random() * (max - min) + min;
  document.querySelector(".line").style.width = rn+'px';
}
body {
  background:#000000; 
}

.line {
  height: 1px;
  width: 40px;
  background: white;
  transform: rotate(45deg);
}
<div id=rain onclick="randomnumber('1','100')" >
        <div class="size">
            <div class="line">
            </div>
        </div>
    
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You cannot use as a parameter of a javascript function a css value such as 100px or something like 1rem.

In the 'rain' div use this code:

//...
<div id="rain" onclick="randomnumber(1, 100)">
    //...
</div>
//...

about 4 years ago · Juan Pablo Isaza Report

0

  • The parameters in your function have to be integers to apply arithmetics on it.
  • If you are setting a width you have to add the unit like px.
  • In your CSS you are setting the width of the element having the class line, but in your JS your are changing the value of the element having the id rain - to change the width you've set in css you can use the querySelector provided below.

function randomnumber(min, max) {
  var rn = Math.random() * (max - min) + min;
  document.querySelector('#rain .line').style.width = rn + 'px';
}
body {
   background:#000000; 
   margin: 100px;
}

.line {
  height: 5px;
  width: 50px;
  background: white;
  transform: rotate(45deg);
}
<div id="rain" onclick="randomnumber(1,100)">
  <div class="size">
    <div class="line">
    </div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!