Posts by bfavaretto • 64,705 points
902 posts
-
8
votes2
answers43
viewsA: How to determine if two random values are next?
Yeah, the way is what @rray commented: function perto($a, $b) { $limite = 2; return abs($a - $b) <= $limite; } if(perto($rand1, $rand2)) { echo 'perto'; }…
phpanswered bfavaretto 64,705 -
3
votes4
answers456
viewsA: Take html from a created variable
Note: had not realized that the question used jQuery, the answer below uses pure JS. I don’t understand why you want to convert everything to string before you leave. If you already have the…
-
3
votes2
answers30
viewsA: Use of Class.apply in the context of a specialized class
Briefly, this apply will execute the parent class constructor in the context of the daughter class. That’s what it does, calls a function forcing a value to this within it. That is, any reference to…
-
6
votes2
answers1804
viewsA: Sort list with Vue
You start with the method sort that arrays have in Javascript. This method returns a function where you define how to compare a pair of values a and b. It must return a value less than zero if a is…
-
2
votes4
answers97
viewsA: Event.clientX; and Event.clientY do not work in firefox
Your code is almost there. Two small changes solve. On the addeventlistener part, pass a reference to the function itself you want to call, instead of using an anonymous function and calling the…
javascriptanswered bfavaretto 64,705 -
11
votes1
answer6117
viewsA: Take elements by class/id with pure Javascript
The modern way in pure JS is: // Para um elemento, por seletor var el = document.querySelector('div.oi'); // Para múltiplos elementos var els = document.querySelectorAll('div.oi'); // Para um…
-
5
votes2
answers376
viewsA: Why is JSON not considered a markup language? And why is XML?
Not always this kind of categorization is so rigid, but in my opinion it would force the bar to want to define JSON as a markup language. I can’t imagine taking an arbitrary document and "marking…
-
4
votes0
answers44
viewsQ: Part of my code doesn’t seem to pass through Babel before arriving at Uglify
I’m not managing to generate the production build of my application with Laravel Mix. When trying to generate the build npm tells me there was an error trying to run Uglifyjs: /js/app.js from…
-
1
votes1
answer138
viewsA: Javascript function for generic use
If I understood correctly, it would be this: function multiplica(tabName) { var quantidade = parseFloat(document.getElementById(tabName + "quantidade").value); var aux =…
javascriptanswered bfavaretto 64,705 -
2
votes5
answers2766
viewsA: What HTTP code should I use when I can’t authenticate to third-party services with login and password provided by the client earlier?
I consider that 403 (Forbidden) makes sense in this case. I’ve already used 400 (Bad request) in similar cases - it’s a little accurate, but I still think acceptable. The justification for the use…
-
0
votes2
answers27
viewsA: Define size in px based on size in %
If I understand well what you want, it is necessary to read the width calculated by browser and set the height based on it. Something like this: var estilos = getComputedStyle(imageHolder); //…
javascriptanswered bfavaretto 64,705 -
5
votes2
answers7451
viewsA: .Empty() in javascript, how to do?
Sergio’s answer is correct, but I’ll leave here an alternative that has better performance - useful if you need to optimize your application. The function below takes an element from the DOM and…
-
4
votes1
answer36
viewsA: How to put a field in a javascript object?
Javascript is a dynamic, weak typing language. One of its characteristics is that variables can store any type of value, and can change types without generating any error or warning. With that in…
javascriptanswered bfavaretto 64,705 -
1
votes2
answers1022
viewsA: How to update a v-for variable in Vue JS?
In Vue, if you dynamically insert a property into a reactive object, she will not be reactive: window.onload = function() { var vm = new Vue({ el: '#app', data: { inventory: [ // Só o 1o item tem a…
-
9
votes2
answers440
viewsA: Why is it not possible to modify local variables when accessed within anonymous classes?
Responding briefly - and I’m no expert on Java -, this is due to the way Java handles closures. What Java makes available to the anonymous class is not the variables themselves (i.e., references to…
javaanswered bfavaretto 64,705 -
1
votes2
answers382
viewsA: Flexbox, can I use without fear in 2017?
There’s nothing you can use without fear, not in 2017, never, especially when it comes to CSS :) Even something seemingly innocent like property opacity can cause unexpected effects depending on how…
-
3
votes2
answers104
viewsA: Javascript going to the top of the page?
Swap the anchor of the Like button for a span. href blank is causing a bearing to the top of the page.
-
2
votes2
answers91
viewsA: How to change option from JSON return
Just set the value of <select> with the value of the desired option: var sel = document.getElementById('modalidade-frete'); sel.value = 1; // no caso, sel.value = data.mod_frete…
-
9
votes4
answers150
viewsA: Basic JS exercise: simple text search
You do not need to do this search "on the nail", character by character. It is easier to use indexOf: var text = "Xxxxx, xxxx x xxxx x xxxx xxxxxx xxxxxxx. Lucas Menezes"; var myName= "Lucas"; var…
javascriptanswered bfavaretto 64,705 -
2
votes1
answer184
viewsA: How to load multiple pages . html with ajax
This line supersedes the entire content of #baseTorrent for what has been: $('#baseTorrent').html(html); From what you say, you want to add more content, not replace. For this, use append():…
-
2
votes1
answer393
viewsA: Switching from Apache to IIS: how to display errors
First, make sure PHP sends errors by including these two lines in your code: error_reporting(E_ALL); ini_set("display_errors", 1); Note that no code (from a framework, for example) undoes this…
-
9
votes2
answers119
viewsQ: ES6 classes do not allow property declaration?
I was experimenting with the class declaration syntax ES6/ES-2015, and could not declare properties, only methods: class Teste { constructor() { } metodo() { } // não funciona: //propriedade: valor…
-
0
votes1
answer121
viewsA: Set Google Analytics event label
All you have to do is change 'interesse' by the value of select at time of Submit: <form action="envia_orca.php" onSubmit="ga('send', 'event', document.getElementById('plano').value,'Solicitar…
-
7
votes4
answers21920
viewsA: How to transform string into character array?
Beyond the split('') that Sergio recommended, it is also possible to access each character directly by index, even without converting to array. For example: var str = 'teste'; str[2] === 'e' //…
-
2
votes1
answer64
viewsA: How do I delete repeated values in an array and count their number of values
$unicos = array_unique($array); $quantidade = count($unicos);
phpanswered bfavaretto 64,705 -
6
votes2
answers115
viewsA: Transition time does not work
This is easier to solve by CSS when defining your classes. For example: $('div').addClass('off'); setTimeout( () => $('div').removeClass('off').addClass('on'), 3000); div { height: 100px;…
-
8
votes4
answers1246
viewsA: Identify if all characters are equal
There are many ways to do this. I don’t think yours is wrong, it’s very clear and didactic. But it has shorter ways, like Mr Felix’s. Converting to array can do some tricks too, for example: var…
-
3
votes1
answer1486
viewsA: place DIV in the center of the page
Try it like this: .dialogbox { position: absolute; top: 30px; left: calc(50vw - 275px); width: 550px; border-radius: 4px; background: #FFFFFF; margin 0; overflow: hidden; z-index: 9999; background:…
cssanswered bfavaretto 64,705 -
2
votes1
answer46
viewsA: Query does not return all record data
If they are fields of the same model/table as you say, it is possible to pass the list of fields you want. For example: $nota = $this->Nota->find('first', [ 'fields' => ['id', 'nome',…
-
6
votes2
answers67
viewsA: How to reverse series of denied conditions without affecting logic?
They are opposite logics. Try to make it clear in Portuguese. For example, the first version says: Se NADA ESTÁ VAZIO: Considera preenchido // Garantido que TODOS estejam PREENCHIDOS Senão:…
-
7
votes3
answers403
viewsA: Best practices in Javascript variable declaration
If you are aware of how the Hoisting works, the biggest implications are for other people who might be messing with your code. I believe that this recommendation is based mainly on the consequences…
-
8
votes3
answers611
viewsA: Variable variable in php
With variable variables would be so: $varname = 'vertical' . $i; $$varname = $v['arquivo']; I find it kind of ugly and confusing, I prefer to use an array: <?php $vv = mysql_query("SELECT * FROM…
-
10
votes3
answers686
viewsA: How to change a text variable to number value?
I like the simplicity of + unary: Total += +P; // Variáveis começam com minúsculas, // o ideal seria: total += +p;…
-
4
votes1
answer100
viewsA: Does redirection not interrupt script interpretation?
The function header PHP only sends an HTTP header to the browser, and the HTTP protocol allows multiple header by request or response. So PHP doesn’t know you’re doing a redirect, it adds all the…
-
2
votes2
answers108
viewsA: Error calling function Rand() in setting a property
You can’t call functions (like rand) in the definition of class properties, only in the constructor. The constructor is a special function executed whenever an instance of the class is created, and…
phpanswered bfavaretto 64,705 -
0
votes2
answers1471
viewsA: Transform object array into only one array
Why not use the objects themselves? foreach($arr as $obj) { echo $obj->key . ': ' . $obj->value; } It is also possible to do cast for array when accessing, if you really want to use array…
-
1
votes2
answers1959
viewsA: Mysql query excluding empty LINES
If you want to check if the line has at least one NULL, you can add up all the numeric columns in question and delete the rows in which that sum is NULL: WHERE (gnbr + vnse + ... + sprd) IS NOT NULL…
mysqlanswered bfavaretto 64,705 -
4
votes2
answers1926
viewsA: Jquery - How to pass a variable to a jquery selector?
Considering your solution, you can still simplify the code well: $( function() { $('#geral :input').click(function(){ // 1. não usar caixa alta em nomes de var // 2. não precisa de jQuery para pegar…
-
2
votes2
answers46
viewsA: How to prevent a command
Simply exit the function after displaying the error message: MessageBox.Show("Preencha o Campo Sexo!"); return; // sai da função
-
5
votes2
answers166
viewsA: What is the Countable interface for in PHP?
Yeah, it’s a standard interface PHP. Serves to make an object compatible with the function count(). If your class implements Countable and has a method count, this method will be invoked when a…
-
26
votes4
answers2317
viewsA: What is the use of Exclamation Mark (!) before declaring functions in Javascript?
Try removing the exclamation to see what happens. Open the console. It will give a syntax error, and the function will not be executed. Javascript syntax interprets the function as a statement only…
javascriptanswered bfavaretto 64,705 -
4
votes5
answers19374
viewsA: How to check if a field of type text or ntext is null or empty?
With COALESCE you get a more elegant solution: SELECT Subject, Notes FROM DocumentNotes WHERE CONVERT(nvarchar, COALESCE(Notes, '')) = '' In the case of SQL Server, the same result can be obtained…
-
2
votes4
answers318
viewsA: How to return an object from a dynamic reference in Javascript?
The easiest is to use an array: // Define class function Test(num) { this.msg = "Message"; this.num = num; } var objetos = []; objetos.push( new Test(1) ); objetos.push( new Test(2) ); objetos.push(…
-
6
votes5
answers2039
viewsA: How to centralize information for all Tds by CSS?
td { text-align: center; /* alinhamento horizontal */ vertical-align: middle; /* alinhamento vertical */ } This rule would apply to all Tds unless some later or more specific rule overrides these…
cssanswered bfavaretto 64,705 -
2
votes2
answers244
viewsA: How to sort array decreasingly?
Two things: The rsort reorders the original array itself, and does not return a new one (returns true or false indicating whether or not the operation was successful). The rsort throw away the keys…
-
3
votes3
answers78
viewsA: Where do the HTML form data go?
In general terms, the following happens:: The form is sent to the server at the address indicated in the attribute action of <form>. A program receives the data in the server environment - in…
htmlanswered bfavaretto 64,705 -
1
votes3
answers2186
viewsA: Error "... is not a recognized built-in Function name" in SQL Server
The function TO_DATE, that you use 3 times, does not exist in SQL Server. Must be a custom function available in another BD, where this code originally came from. As GOKU taught, it is an Oracle…
sql-serveranswered bfavaretto 64,705 -
2
votes1
answer279
viewsA: pull information via php ajax
There are several small errors in your code, try this modified version: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <form>…
-
5
votes1
answer91
viewsA: Mini search engine
If I understand the question correctly, you want it: $matches = array(); if(exec('grep '.escapeshellarg($search).' ./urls.txt', $matches)) { // $matches contém as linhas encontradas } Just pass to…
-
5
votes2
answers188
viewsA: What is the difference between the following ways of using the jQuery ON and OFF methods
Yes, the second form is correct, according to the documentation. She removes all the listeners events that have been added with on - in the case of your example, all listeners of the document will…
jqueryanswered bfavaretto 64,705