Posts by Luiz Felipe • 32,886 points
746 posts
-
2
votes3
answers102
viewsA: Javascript performs the function without the button being clicked
The problem is that you are not assigning the function to the event onclick, but by invoking the function. In addition, there is also a typo. See: btn.onClick = mudaTexto(); What you need to do is,…
-
1
votes2
answers331
viewsA: Adonis make:model generating model with different name
There is no way to "solve" this, since it is not a mistake, but a convention. Like other frameworks such as Ruby on Rails or Laravel, Adonisjs has a number of conventions regarding how your code…
-
0
votes1
answer102
viewsQ: Is there a performance gain in concatenating strings directly into SQL?
Actually, I have a code kind of like this: const users = await query(` SELECT "id", "username", "email" FROM "users"; `) .then((users) => users.map((user) => { user.id =…
-
5
votes2
answers86
viewsQ: How does ORDER BY draw columns in case of a repeated value?
Suppose I make one SELECT sort of like this: SELECT * FROM "users" ORDER BY "createdAt"; In this context, if I have data that has the same value in the field createdAt, how SQL will sort these…
-
2
votes1
answer54
viewsA: I want to draw a random number and compare it with an array index number
Once you have loaded JSON, which I will assume is a list of objects, just choose a random item from that list. Something like that: // Após carregar, você terá algo assim: const json = [ { id: 'A'…
-
5
votes1
answer53
viewsA: Operation of "Alert" in javascript
If you have text in your variable $h, the code generated by PHP will be something like this: alert(Caracteres); // ↑↑↑↑↑↑↑↑↑↑ // Sintaxe Inválida Invalid in Javascript syntax. You need to treat it…
javascriptanswered Luiz Felipe 32,886 -
2
votes1
answer396
viewsA: How to use queryselector in an input?
The textContent is used to get the text node that can be inside an HTML element. To get the value of a input, select or textarea, you must use the property value. Thus: const field =…
javascriptanswered Luiz Felipe 32,886 -
3
votes1
answer281
viewsA: Why doesn’t the required attribute work?
As said by Augusto Vasques in your comment, the attribute required only perform the verification in the submission of the form. It is worth noting that after this validation, the event submit of a…
-
0
votes2
answers790
viewsA: How can I use a handlebar variable inside a script
Try to convert the object into a string before: <script> console.log({{ JSON.stringify(estado) }}); </script>
-
0
votes1
answer150
viewsA: How to show Laravel Storage image with Ajax?
If your controller directly serves an image file, you can create an Blob, using the fetch, and then generate a URL. In the example below, the URL we are requesting is any image file. But in your…
-
1
votes2
answers40
viewsA: How to return a value from a checked column?
You can do something like this: const $inputs = $('#table-data td input'); const $button = $('#save'); $button.on('click', () => { const ids = []; $inputs.each(function() { const $input =…
-
0
votes1
answer62
viewsA: Emulate navigation
You can use the tool Puppeteer, which is basically a Google Chrome with an interface exposed in Javascript methods. With a simple script you can collect information from any web page (as long as it…
javascriptanswered Luiz Felipe 32,886 -
1
votes1
answer77
viewsA: How to run a Script using AJAX
You can use the method document.createElement to create the <script> and then insert it into the body of the page: function createScript(src, attrs = {}) { // Criamos o `<script>`: const…
-
3
votes1
answer594
viewsQ: Where do the names "atob" and "btoa" come from?
In Javascript, for example, we use the functions atob and btoa to manage codes in base64, decoding and encoding, respectively. The question is, where do these names come from?…
-
2
votes2
answers1505
viewsA: Clickable image redirecting to another HTML page
No need to use Javascript in this case If you just want to redirect to another page, you don’t even need Javascript, just using the attribute href: <a href="/users/69296"> <img…
-
1
votes1
answer62
viewsA: How to improve async in this case
The method forEach array is not ideal to deal with promises. To deal with promise lists, I suggest you use the Array.prototype.map with Promise.all or use a repeat loop for within an asynchronous…
-
2
votes1
answer31
viewsA: Why does the alert prevent HTML from being displayed when the page is loading?
In fact the HTML of <h1> and <p> are loaded before the <script>. However, the <script> in this case it was executed before the HTML was "painted" on the page, and as the…
javascriptanswered Luiz Felipe 32,886 -
1
votes1
answer107
viewsA: How to detect if the device is touch in pure javascript?
You can check if the event touch that you want exists in the global object window. For example, if you are going to use the event ontouchstart, you can do so: if ('ontouchstart' in window) { //…
-
0
votes1
answer155
viewsA: Create and populate elements with json return in jquery
You have several ways to do this. Using the Github API repositories, we can make an example: const API_URL = 'https://api.github.com/users/lffg/repos?sort=updated'; function generateHTML(data) {…
-
4
votes2
answers1326
viewsQ: How to work with timezones without using a timestamp?
I was reading that answer, of the Stack Overflow in English, and I came across the following excerpt: For Mysql (or Mariadb), if you don’t need the time information consider using the DATE column…
-
4
votes2
answers55
viewsA: Create function to make calculations with a given operator using Javascript
Not to use the eval, that can bring some risks, you can create a function that makes use of the switch to determine the operation. Something like that: function operate(x, y, op) { switch (op) {…
-
0
votes1
answer235
viewsA: Error using Mysql Sequelize
Adapted from Stack Overflow in English: If you are using Mysql 8x, the problem is in the new form of authentication, which is not supported by most drivers current. To fix, you must modify the…
-
0
votes1
answer550
viewsQ: Queues (queues) are useful in Node.js, which is asynchronous?
I know that in languages like PHP, queues (queues) are often used to perform more "heavy" actions, such as sending a series of emails and the like. Therefore, taking into account the asynchronous…
-
2
votes1
answer52
viewsQ: How to make Git case sensitive?
My operating system is not case sensitive by default, which means that there is no difference between capitalizing the letters of a filename, for example. This behavior constantly gets in my way…
-
4
votes4
answers1946
viewsA: Incorrect Date/Time Formatting Nan/Nan/Nan Nan:Nan:Nan
You can simplify your script. You can do it like this: const test = '/Date(1566322265000)/'; function formatDate(dateString) { const [, ms] = dateString.match(/\((\d+)\)\/$/); const instance = new…
-
4
votes1
answer504
viewsA: How to add an event to all elements with the same class?
The method querySelectorAll returns an object NodeList, who does not have the method addEventListener on your prototype. It means that you cannot add a Listener event to all elements of a NodeList…
javascriptanswered Luiz Felipe 32,886 -
10
votes2
answers388
viewsA: Asynchronous return of some Apis
TL;DR: The methods in question do not come from the API, but from the object Response which is returned as a promise by fetch. To get the data from the body of the answer, you must use methods such…
-
6
votes1
answer68
viewsQ: What is the numbering next to the Unix commands?
I often see command descriptions like this: Like which(1) Unix command. Find the first instance of an Executable in the PATH. [Source] Or this: A cat(1) clone with Wings. [Source] What are these…
-
3
votes1
answer49
viewsA: Error using async in Nodejs
The problem is the version of Node.js that you are running on your server. Resources like async/await entered from Node.js 7.6. That way, since you are using the version 6, these features have not…
-
2
votes1
answer31
viewsQ: Is storing my application logs on disk a bad idea?
We all know the importance of logs to the debug an application in a production environment. Currently, I maintain the logs in an archive (/tmp/logs/app-logs.log). The problem is that it can get…
logasked Luiz Felipe 32,886 -
2
votes1
answer213
viewsA: Treating error in callback function
The error shown in the terminal is being created on the following line: if (err) throw new Error('Can\'t find the file'); Note that you are not treating the error in the most usual way since you are…
-
11
votes1
answer1878
viewsQ: How does String.prototype.normalize work in Javascript?
I was reading a reply here on the site and I came across the method String.prototype.normalize in the second example of code passed. I had already come across this method in another situation, but…
-
7
votes5
answers3692
viewsA: create "contain" function that says if an array contains a certain element and returns true
Checking the existence of a primitive type If you want to verify the existence of a type primitive, can use the indexOf or includes: const list = [1, 2, 3, 4, 5]; console.log(list.indexOf(9)); // -1…
-
5
votes2
answers191
viewsA: Typescript - Incorrect code output - Date
TL;DR: There is nothing wrong and the code works perfectly. In fact, the "problem" is due to the behavior of Javascript when dealing with a date. If you mess with the Javascript date object a little…
-
6
votes3
answers3708
viewsQ: What is the difference between useMemo and React useCallback?
I know that useMemo and useCallback are two new Hooks React. However, I still don’t quite understand the difference between them. What’s the difference between "return a memoised value" and "return…
-
6
votes2
answers316
viewsA: Autofocus for next field with Javascript + Maxlength
Just create a list of all inputs that exist in your form. For this, you can use: const allFields = document.querySelectorAll('form input'); So I would do like this: const allFields =…
javascriptanswered Luiz Felipe 32,886 -
11
votes4
answers333
viewsA: How to manipulate these objects with Javascript?
You could use methods like the reduce, together with other Javascript features. The logic is simple: With each iteration, we want to add to the accumulator object (acc) a property whose key will be…
-
1
votes1
answer340
viewsA: I am trying to consume an api in js via get by Xios
According to the API of Axios, you must pass a configuration object as the second argument, containing the property params with the parameters you wish to pass. Thus remaining: axios.get('/user', {…
-
1
votes2
answers61
viewsA: Catch value in Javascript/Jquery loop
The problem is, with the line down: var teste = $(elementos[i].className).attr('alt'); You are selecting the div where the image is contained. So, when you capture the attribute alt of this element,…
-
0
votes1
answer91
viewsA: How do I make a button submit a form and play a sound
A good approach is to use setTimeout to submit the form after a certain time (after playing the audio, for example). You can do something like this: const audio = new…
-
4
votes1
answer290
viewsA: Why does this function not return values
What happens is that you are inside a function of a callback, then the return that you are using will not return a function value retornaInformacoes, but of the callback: function…
-
1
votes2
answers318
viewsA: Sum values from different JSON indices
I arrived at a solution "dynamics" using only iteration methods of the prototype of Array: const data = [{ itemIndex: 0, selectedSla: 'Expressa', selectedDeliveryChannel: 'delivery', slas: [ { name:…
-
1
votes1
answer54
viewsA: .htcaccess 301 redirect does not work
It is not working because you are commenting on the lines that "do the job" (lines 3 to 5). Then try to remove the symbols # from the beginning of these lines: <IfModule mod_rewrite.c>…
-
2
votes2
answers67
viewsA: select input according to previous option
To save the large amount of ifs, you can make use of the attributes data-* doing something like that: function apply(type) { $('.type[data-type]').hide(); // Esconde todos os campos.…
-
3
votes1
answer45
viewsA: Validation maintaining status in requests
TL;DR: Your problem boils down in your module Error, using a Singleton. The problem arises in the fact that you are using a pattern called Singleton (learn more in this excellent video of Waldemar…
javascriptanswered Luiz Felipe 32,886 -
1
votes1
answer375
viewsA: Join objects with equal values with Javascript
As mentioned in the comments, in Javascript, you cannot create an object that has two properties that share the same key. For example: console.log({ name: 'Unknown', name: 'Luiz Felipe', age: 16 });…
-
8
votes3
answers189
viewsA: Why do we not receive an error when we call an undeclared variable as a window property?
We know that when we try to read a variable that is not available in the current scope the error (ReferenceError) is launched. To learn more, read this document. We also know that when a variable is…
-
7
votes3
answers4742
viewsA: Convert string to dd/mm/yyyy hh:mm format
You need to pass a date string in a valid format. According to the documentation, between the various forms of instantiating a date, there is the option to pass a string. In this case: A value of…
-
4
votes1
answer50
viewsA: Problem validating whether or not it is a number
TL;DR: Use the method Number.isNaN. You’re trying to use the builder Number together with typeof to validate a number. However, this approach does not work, since: The builder Number returns NaN…
javascriptanswered Luiz Felipe 32,886 -
4
votes4
answers210
viewsA: Get post ID with Javascript
To get a list of all the Ids of the elements that have class that starts with post-, just do something like this: const postIds = Array.from( document.querySelectorAll('article[class*="post-"]')…
javascriptanswered Luiz Felipe 32,886