Categories
JavaScript Answers

How to submit form using an a tag with JavaScript?

To submit form using an a tag with JavaScript, we can call the form’s submit method.

For instance, we write

<form name="myForm" action="handle-data.php">
  Search: <input type="text" name="query" />
  <a href="javascript: submitform()">Search</a>
</form>

to add a form.

Then we write

function submitform() {
  document.myForm.submit();
}

to define the submitForm function which we set as the value of the href attribute of the a element.

In it, we get the by its name with document.myForm.

And then we call submit to submit the form.

Conclusion

To submit form using an a tag with JavaScript, we can call the form’s submit method.

Categories
JavaScript Answers

How to add meta tag in JavaScript?

Sometimes, we want to add meta tag in JavaScript.

In this article, we’ll look at how to add meta tag in JavaScript.

How to add meta tag in JavaScript?

To add meta tag in JavaScript, we call createElement.

For instance, we write

const meta = document.createElement("meta");
meta.httpEquiv = "X-UA-Compatible";
meta.content = "IE=edge";
document.head.appendChild(meta);

to call createElement to create a meta element.

Then we set the http-equiv attribute with

meta.httpEquiv = "X-UA-Compatible";

We set the content attribute with

meta.content = "IE=edge";

And we append it as the last child of the head element with

document.head.appendChild(meta);

Conclusion

To add meta tag in JavaScript, we call createElement.

Categories
JavaScript Answers

How to search inside a JSON object with JavaScript?

Sometimes, we want to search inside a JSON object with JavaScript.

In this article, we’ll look at how to search inside a JSON object with JavaScript.

How to search inside a JSON object with JavaScript?

To search inside a JSON object with JavaScript, we use the array find method.

For instance, we write

const data = {
  list: [
    { name: "My Name", id: 1, type: "car owner" },
    { name: "My Name 2", id: 2, type: "car owner 2" },
    { name: "My Name 3", id: 3, type: "car owner 3" },
    { name: "My Name 4", id: 4, type: "car owner 4" },
  ],
};

const result = data.list.find((record) => record.name === "My Name");

to get the data.list array property.

Then we use the find method to get the first result that has the name property equals 'My Name'.

Conclusion

To search inside a JSON object with JavaScript, we use the array find method.

Categories
JavaScript Answers

How to find the parent element using JavaScript?

Sometimes, we want to find the parent element using JavaScript.

In this article, we’ll look at how to find the parent element using JavaScript.

How to find the parent element using JavaScript?

To find the parent element using JavaScript, we use the parentNode property.

For instance, we write

const parent = element.parentNode;

to use the parentNode to get the element‘s parent element.

Conclusion

To find the parent element using JavaScript, we use the parentNode property.

Categories
JavaScript Answers

How to preview images before upload with JavaScript?

To preview images before upload with JavaScript, we use the FileReader constructor.

For instance, we write

<input type="file" />

to add the file input.

Then we write

const readURL = (file) => {
  return new Promise((res, rej) => {
    const reader = new FileReader();
    reader.onload = (e) => res(e.target.result);
    reader.onerror = (e) => rej(e);
    reader.readAsDataURL(file);
  });
};

const preview = async (event) => {
  const [file] = event.target.files;
  const url = await readURL(file);
  const img = document.querySelector("img");
  img.src = url;
};

const fileInput = document.querySelector("input");
fileInput.addEventListener("change", preview);

to call querySelector to get the file input.

And then we call addEventListener to listen for the change event with the preview listener function.

In preview we get the select file with event.target.files

Then we call the readURL function to read the file as a base64 URL.

We call readAsDataURL to get the base64 result.

We call res with e.target.result to return the base64 URL string with the promise.

In preview, we call querySelector to get the img element.

And we set the img.src attribute’s value to url to show the image we get with await.