Showing posts with label Java Script. Show all posts
Showing posts with label Java Script. Show all posts

Simulate a Mouse Hover Using pure JavaScript

 var element = document.getElementById('name');

element.addEventListener('mouseover', function() {

  console.log('Event triggered');

});


var event = new MouseEvent('mouseover', {

  'view': window,

  'bubbles': true,

  'cancelable': true

});


element.dispatchEvent(event);

AJAX from vanilla javascript

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET""ajax_info.txt"true);
  xhttp.send();
}

Get element by part of Name or ID

Q: Get an elements if it's id matches + javascript

A:
document.querySelectorAll('input[id^="id_qtedje_"]');

document.querySelector('[id^="poll-"]').id;
The selector means: get an element where the attribute [id] begins with the string "poll-".

^ matches the start
* matches any position
$ matches the end

How can I get query string values in JavaScript?

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var foo = getParameterByName('foo');

How to set cell or column width when export .xlsx files with js - xlsx + excel + java script + workbook

var wscols = [
    {wpx:6},
    {wpx:7},
    {wpx:10},
    {wpx:20}
];

ws['!cols'] = wscols;

JavaScript Replace anything but numbers from a string

var s = "+1 (444) 7444";

s.replace(/[^0-9.]/g, "");

14447444


Mostly useful for formating phone numbers. There are better JS frame works to format a number based on country code as well, but this seems the lightest.

Find overlap between two strings + Java Script

function findOverlap(a, b) {
  if (b.length === 0) {
    return "";
  }
 
  if (a.endsWith(b)) {
    return b;
  }
 
  if (a.indexOf(b) >= 0) {
    return b;
  }
 
  return findOverlap(a, b.substring(0, b.length - 1));
}
Some test cases:
findOverlap("12345", "aaa")
""
findOverlap("12345", "12")
"12"
findOverlap("12345", "345")
"345"
findOverlap("12345", "3456")
"345"
findOverlap("12345", "111")
"1"

How to use a custom data strucutre or data type defined in java script

// From a length
var uint16 = new Uint16Array(2);
uint16[0] = 42;
console.log(uint16[0]); // 42
console.log(uint16.length); // 2
console.log(uint16.BYTES_PER_ELEMENT); // 2
Uint16Array is defined as data structure or data type, then we can use it as 

var uint16 = new Uint16Array();

Firefox ignores option selected=“selected”, Selected = '"

This behaviour is hard coded in FF.

Add autocomplete="off" HTML attribute to the respective select tag.

Get attribute of SPAN using JavaScript

<span id='abc' email='xyz@gmail.com'> xyz </span>


How to get attribute of SPAN using JavaScript

document.getElementById('abc').getAttribute('email')


How to SPAN content or html or text using JavaScript

document.getElementById('abc').innerHTML

document.getElementById('abc').textContent

Detect when url changed java script or jquery

$(window).on('hashchange', function(e){
    var origEvent = e.originalEvent;
    alert(e);
    console.log('Going to: ' + origEvent.newURL + ' from: ' + origEvent.oldURL);
});

Find element in DOM by mutliple classes

If you want an intersection, just write the selectors together without spaces in between.
$('.a.b')
So for an element that has an ID of a with classes b and c, you would write:
$('#a.b.c')

Get Or Find Element in DOM by multiple attributes, role and class

<div id="mydiv">mydiv
<div role="complementary" class="nH abC">myclass1</div>
<div role="complementary" class="nB abd">myclass2</div>
</div>


$(".nH").css('background-color','red');
$('complementary, .abC').css('background-color','green');

How to check if String contains Sub String with Java Script or Jquery

Case Sensitive
if (originalString.indexOf("substring") >= 0)

Case In-Sensitive
if (originalString.toLowerCase().indexOf("substring") >= 0)
Or,
if (/substring/i.test(originalString))