Signup/Sign In

Answers

All questions must be answered. Here are the Answers given by this user in the Forum.

***
var arr = [23, 45, 12, 67];
arr = [34, ...arr]; // RESULT : [34,23, 45, 12, 67]

console.log(arr)
***
3 years ago
The libraries go to some lengths to get accurate offsets for an element.
here's a simple function that does the job in every circumstances that I've tried.
***
function getOffset( el ) {
var _x = 0;
var _y = 0;
while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
_x += el.offsetLeft - el.scrollLeft;
_y += el.offsetTop - el.scrollTop;
el = el.offsetParent;
}
return { top: _y, left: _x };
}
var x = getOffset( document.getElementById('yourElId') ).left;
***
3 years ago
***
function isMobile() { return ('ontouchstart' in document.documentElement); }
***
However above code doesn't take into account the case for laptops with touchscreen. Thus, I provide this second version,
***
function isMobile() {
try{ document.createEvent("TouchEvent"); return true; }
catch(e){ return false; }
}
***
3 years ago
TypeScript does something similar to what less or sass does for CSS. They are super sets of it, which means that every JS code you write is valid TypeScript code. Plus you can use the other goodies that it adds to the language, and the transpiled code will be valid js. You can even set the JS version that you want your resulting code on.

Currently TypeScript is a super set of ES2015, so might be a good choice to start learning the new js features and transpile to the needed standard for your project.
3 years ago
It's looking for an element with id list which has a property value equal to 2.
What you want is the option child of the list:
***
$("#list option[value='2']").text()
***
3 years ago
***
function isScrolledIntoView(el) {
var rect = el.getBoundingClientRect();
var elemTop = rect.top;
var elemBottom = rect.bottom;

// Only completely visible elements return true:
var isVisible = (elemTop >= 0) && (elemBottom <= window.innerHeight);
// Partially visible elements return true:
//isVisible = elemTop < window.innerHeight && elemBottom >= 0;
return isVisible;
}
***
3 years ago
I've been using jquery-json for 6 months and it works great. It's very simple to use:
***
var myObj = {foo: "bar", "baz": "wockaflockafliz"};
$.toJSON(myObj);
***
// Result: {"foo":"bar","baz":"wockaflockafliz"}
3 years ago
From the jQuery documentation: you specify the asynchronous option to be false to get a synchronous Ajax request. Then your callback can set some data before your mother function proceeds.

Here's what your code would look like if changed as suggested:
***
beforecreate: function (node, targetNode, type, to) {
jQuery.ajax({
url: 'example_url/catalog/create/' + targetNode.id + '?name=' + encode(to.inp[0].value),
success: function (result) {
if (result.isOk == false) alert(result.message);
},
async: false
});
}
***
3 years ago
Set the values for each of the options
***

***
$('#aioConceptName').val() didn't work because .val() returns the value attribute. To have it work properly, the value attributes must be set on each
3 years ago
***
$('#mySelect')
.find('option')
.remove()
.end()
.append('')
.val('whatever')
;
***
3 years ago
***
$('*[data-customerID="22"]');
***
You should be able to omit the *, but if I recall correctly, depending on which jQuery version you’re using, this might give faulty results.

Note that for compatibility with the Selectors API (document.querySelector{,all}), the quotes around the attribute value (22) may not be omitted in this case.

Also, if you work with data attributes a lot in your jQuery scripts, you might want to consider using the HTML5 custom data attributes plugin. This allows you to write even more readable code by using .dataAttr('foo'), and results in a smaller file size after minification (compared to using .attr('data-foo')).
3 years ago
If you want to respond to an event just one time, the following syntax should be really helpful:
***
$('.myLink').bind('click', function() {
//do some things

$(this).unbind('click', arguments.callee); //unbind *just this handler*
});
***
Using arguments.callee, we can ensure that the one specific anonymous-function handler is removed, and thus, have a single time handler for a given event. Hope this helps others.
3 years ago