Categories
JavaScript Answers

How to Disable Clicking Inside a Div with CSS or JavaScript?

To disable clicking inside a div with CSS or JavaScript, we can set the pointer-events CSS property to none .

Also, we can add a click event listener to the div and then call event.preventDefault inside.

For instance, if we have the following div:

<div id='foo'>  
  foo  
</div>

Then we can disable clicks inside it with CSS by writing:

#foo {  
  pointer-events: none;  
}

To disable clicks with JavaScript, we can write:

const foo = document.querySelector('#foo')  
foo.addEventListener('click', (event) => {  
  event.preventDefault();  
});

to select the div by its ID with document.querySelector .

Then we call addEventListener on it with 'click' and a click event handler that calls event.preventDefault to stop the default action when the div is clicked.

Categories
JavaScript Answers

How to Do Gaussian or Banker’s Rounding in JavaScript?

Sometimes, we want to do Gaussian or Banker’s rounding in JavaScript.

In this article, we’ll look at how to do Gaussian or Banker’s rounding in JavaScript.

Do Gaussian or Banker’s Rounding in JavaScript

To do Gaussian or Banker’s rounding in JavaScript, we can create our own function to do the rounding.

For instance, we write:

const isEven = (n) => {
  return (0 === (n % 2));
};

const bankersRound = (x) => {
  const r = Math.round(x);
  return (((((x > 0) ? x : (-x)) % 1) === 0.5) ? ((isEven(r)) ? r : (r - 1)) : r);
};

console.log(bankersRound(1))
console.log(bankersRound(1.5))
console.log(bankersRound(2.5))

We first create the isEven function to return whether the number n is an even number.

Then we create the bankersRound function to round the number.

In the function, we first round the number x with Math.round .

Then we check if x is bigger than 0.

If it is, then we return x .

Otherwise, we return -x .

And we check if that number divided by 1 has 0.5 as the remainder.

If it does, we check if the number if even with isEven .

If it is, we return r .

Otherwise, we return r — 1 .

If x ends with .5, then we also return r .

Therefore, we see the first console log logs 1 and the other 2 logs 2.

Conclusion

To do Gaussian or Banker’s rounding in JavaScript, we can create our own function to do the rounding.

Categories
Express JavaScript Answers

How to Render a Basic HTML View in a JavaScript Express App?

Sometimes, we want to render a basic HTML view in a JavaScript Express app.

In this article, we’ll look at how to render a basic HTML view in a JavaScript Express app.

Render a Basic HTML View in a JavaScript Express App

To render a basic HTML view in a JavaScript Express app, we can install a templating library that works with Express.

For instance, we can write:

const express = require('express')
const app = express()
const port = 3000
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');

app.get("/", (req, res) => {
  res.render('about.html');
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

to add the ejs library by installing:

npm i ejs

Then we write:

app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');

to serve the views folder to serve that has the template folder.

We specify that we use the ejs templating engine with the last 2 lines.

Then in the / route, we call res.render with 'about.html' to serve the content of that as the template.

In about.html , we have:

about

as its content.

And so when we go to / , we see about displayed.

Conclusion

To render a basic HTML view in a JavaScript Express app, we can install a templating library that works with Express.

Categories
Express JavaScript Answers

How to Get All Registered Routes in Express and JavaScript?

Sometimes, we want to get all registered routes in Express and JavaScript.

In this article, we’ll look at how to get all registered routes in Express and JavaScript.

Get All Registered Routes in Express and JavaScript

To get all registered routes in Express and JavaScript, we can use the app._route.stack property.

For instance, we can write:

const express = require('express')
const app = express()
const port = 3000

app.get("/", (req, res) => {
  res.send('hello world')
});

app.get("foo", (req, res) => {
  res.send('foo')
});

app.get("bar", (req, res) => {
  res.send('bar')
});

app._router.stack.forEach((r) => {
  console.log(r.route && r.route.path)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

to log all the registered routes with:

app._router.stack.forEach((r) => {
  console.log(r.route && r.route.path)
})

We get the pathname string with r.route.path .

Conclusion

To get all registered routes in Express and JavaScript, we can use the app._route.stack property.

Categories
JavaScript Answers jQuery

How to Determine the Pixel Length of a String in JavaScript?

Sometimes, we want to determine the pixel length of a string in JavaScript.

In this article, we’ll look at how to determine the pixel length of a string in JavaScript.

Determine the Pixel Length of a String in JavaScript

To determine the pixel length of a string in JavaScript, we can put the string in a span and then use the jQuery width method to determine the width of the span.

For instance, we can write the following HTML:

<span></span>

Then we can write:

const span = document.querySelector('span')  
span.innerText = 'Here is my string'  
console.log($(span).width())

to get the span width document.querySelector .

And then we set innerText to the string of the span.

Finally, we log the span’s width that we get from $(span).width() from jQuery.

The console log should log 111.

Conclusion

To determine the pixel length of a string in JavaScript, we can put the string in a span and then use the jQuery width method to determine the width of the span.