Categories
HTML

How to store and retrieve JavaScript arrays into and from HTML5 data attributes?

Sometimes, we want to store and retrieve JavaScript arrays into and from HTML5 data attributes.

In this article, we’ll look at how to store and retrieve JavaScript arrays into and from HTML5 data attributes.

How to store and retrieve JavaScript arrays into and from HTML5 data attributes?

To store and retrieve JavaScript arrays into and from HTML5 data attributes, we can put the array directly in the element.

For instance, we write

<div id="demo" data-stuff='["some", "string", "here"]'></div>

to set the data-stuff attribute to the ["some", "string", "here"] array string.

Conclusion

To store and retrieve JavaScript arrays into and from HTML5 data attributes, we can put the array directly in the element.

Categories
JavaScript Answers

How to pass a JavaScript variable value into input type hidden value?

Sometimes, we want to pass a JavaScript variable value into input type hidden value.

In this article, we’ll look at how to pass a JavaScript variable value into input type hidden value.

How to pass a JavaScript variable value into input type hidden value?

To pass a JavaScript variable value into input type hidden value, we can set its value property.

For instance, we write

<input type="hidden" id="myField" value="" />

to add a hidden input.

Then we write

document.getElementById("myField").value = product(2, 3);

to select the input with getElementById.

Then we set its value property to the return value of the product function to set the value attribute of the input.

Conclusion

To pass a JavaScript variable value into input type hidden value, we can set its value property.

Categories
JavaScript Answers

How to include a hyphen in a regex character bracket with JavaScript?

Sometimes, we want to include a hyphen in a regex character bracket with JavaScript.

In this article, we’ll look at how to include a hyphen in a regex character bracket with JavaScript.

How to include a hyphen in a regex character bracket with JavaScript?

To include a hyphen in a regex character bracket with JavaScript, we can use \-.

For instance, we write

/^[a-zA-Z0-9.\-_]+$/

to add hyphen into the list of characters to match in the square brackets.

Conclusion

To include a hyphen in a regex character bracket with JavaScript, we can use \-.

Categories
JavaScript Answers

How to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript?

Sometimes, we want to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript.

In this article, we’ll look at how to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript.

How to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript?

To use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript, we can use spies.

For instance, we write

class Klass {
  method(arg) {
    return arg;
  }
}

describe("spy behavior", () => {
  it("should spy on an instance method of a Klass", () => {
    const obj = new Klass();
    spyOn(obj, "method");
    obj.method("foo argument");
    expect(obj.method).toHaveBeenCalledWith("foo argument");
    expect(
      obj.method.calls.mostRecent().args[0] instanceof String
    ).toBeTruthy();
  });
});

to create the Klass class to test.

Then we create a new Klass instance in the test.

We then spy on the method method with spyOn(obj, "method");.

Next, we call the method with obj.method("foo argument");.

Then we check that obj.method is called with 'foo argument' with expect(obj.method).toHaveBeenCalledWith("foo argument");.

And then we check that it’s most recently called with a string with

expect(obj.method.calls.mostRecent().args[0] instanceof String).toBeTruthy();

Conclusion

To use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript, we can use spies.

Categories
JavaScript Answers

How to close all info windows in Google Maps API v3 and JavaScript?

Sometimes, we want to close all info windows in Google Maps API v3 and JavaScript.

In this article, we’ll look at how to close all info windows in Google Maps API v3 and JavaScript.

How to close all info windows in Google Maps API v3 and JavaScript?

To close all info windows in Google Maps API v3 and JavaScript, we can call the close method.

For instance, we write

const latlng = new google.maps.LatLng(-34.397, 150.644);
let infoWindow = null;

//..

google.maps.event.addListener(marker, "click", () => {
  infoWindow?.close();
  infoWindow = new google.maps.InfoWindow();
  //...
});

to call infoWindow?.close() to close the info window when we click on a marker.

Then we create a new info window with

infoWindow = new google.maps.InfoWindow();

Conclusion

To close all info windows in Google Maps API v3 and JavaScript, we can call the close method.