Categories
JavaScript Answers

How to pick element inside iframe using document.getElementById with JavaScript?

Sometimes, we want to pick element inside iframe using document.getElementById with JavaScript.

In this article, we’ll look at how to pick element inside iframe using document.getElementById with JavaScript.

How to pick element inside iframe using document.getElementById with JavaScript?

To pick element inside iframe using document.getElementById with JavaScript, we can use the contentWindow property.

For instance, we write

const el = document
  .getElementById("myframe1")
  .contentWindow.document.getElementById("x");

to select the iframe with getElementById.

Then we use contentWindow.document.getElementById to select the element with ID x in the iframe.

Conclusion

To pick element inside iframe using document.getElementById with JavaScript, we can use the contentWindow property.

Categories
JavaScript Answers

How to clear spy programmatically in Jasmine and JavaScript?

Sometimes, we want to clear spy programmatically in Jasmine and JavaScript.

In this article, we’ll look at how to clear spy programmatically in Jasmine and JavaScript.

How to clear spy programmatically in Jasmine and JavaScript?

To clear spy programmatically in Jasmine and JavaScript, we can just change the spy.

For instance, we write

let spyObj;
beforeEach(() => {
  spyObj = spyOn(obj, "methodName").and.callFake((params) => {});
});

it("should do the declared spy behavior", () => {
  // ...
});

it("should do what it used to do", () => {
  spyObj.and.callThrough();
});

it("should do something differently", () => {
  spyObj.and.returnValue("new value");
});

to create the spyObj object with spyOn.

Then we write spyObj.and.callThrough(); to call obj.methodName in the first test.

In the 2nd test, we call spyObj.and.returnValue to return a new mocked value for obj.methodName.

The existing spy will be cleared after each test.

Conclusion

To clear spy programmatically in Jasmine and JavaScript, we can just change the spy.

Categories
JavaScript Answers

How to check if element is a div with JavaScript?

Sometimes, we want to check if element is a div with JavaScript.

In this article, we’ll look at how to check if element is a div with JavaScript.

How to check if element is a div with JavaScript?

To check if element is a div with JavaScript, we can check the tagName property of the element.

For instance, we write

const myElement = document.getElementById("myElementId");

if (myElement.tagName === "DIV") {
  alert("is a div");
} else {
  alert("is not a div");
}

to select the element with getElementById.

Then we check if the myElement.tagName property returns 'DIV'.

If it is, then myElement is a div.

Conclusion

To check if element is a div with JavaScript, we can check the tagName property of the element.

Categories
HTML

How to submit a form when the return key is pressed with HTML?

Sometimes, we want to submit a form when the return key is pressed with HTML.

In this article, we’ll look at how to submit a form when the return key is pressed with HTML.

How to submit a form when the return key is pressed with HTML?

To submit a form when the return key is pressed with HTML, we can add an input with type attribute set to submit.

For instance, we write

<form action="" method="get">
  Name: <input type="text" name="name" /><br />
  Pwd: <input type="password" name="password" /><br />
  <div class="yourCustomDiv" />
  <input type="submit" style="display: none" />
</form>

to add an input with type set to submit in our form.

Then when we press enter, a submission is made by the form.

Conclusion

To submit a form when the return key is pressed with HTML, we can add an input with type attribute set to submit.

Categories
JavaScript Answers

How to open window in JavaScript with HTML inserted?

Sometimes, we want to open window in JavaScript with HTML inserted.

In this article, we’ll look at how to open window in JavaScript with HTML inserted.

How to open window in JavaScript with HTML inserted?

To open window in JavaScript with HTML inserted, we can set the innerHTML property of the window’s body element.

For instance, we write

const win = window.open(
  "",
  "Title",
  "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=200,top=" +
    (screen.height - 400) +
    ",left=" +
    (screen.width - 840)
);
win.document.body.innerHTML = "HTML";

to call window.open with a string with the dimensions and other settings of the window.

Then we set win.document.body.innerHTML to the HTML string we want to render in the window.

Conclusion

To open window in JavaScript with HTML inserted, we can set the innerHTML property of the window’s body element.