Categories
JavaScript Answers

How to get the paragraph of selected text in web page with JavaScript?

Sometimes, we want to get the paragraph of selected text in web page with JavaScript.

In this article, we’ll look at how to get the paragraph of selected text in web page with JavaScript.

How to get the paragraph of selected text in web page with JavaScript?

To get the paragraph of selected text in web page with JavaScript, we can use the window.getSelection method.

For instance, we write:

<p>
  hello world
</p>
<button>
  get selection
</button>

to add a p and button elements.

Then we write:

const button = document.querySelector('button')
button.onclick = () => {
  const x = window.getSelection()
  const z = x.anchorNode.parentNode
  console.log(z.innerHTML)
}

to select the button with querySelector.

And then we set button.onclick to a function that gets the selection from the page.

And we get the node with the selected content with x.anchorNode.parentNode.

Finally, we get the innerHTML from the node with the selected text.

Conclusion

To get the paragraph of selected text in web page with JavaScript, we can use the window.getSelection method.

Categories
JavaScript Answers

How to hide mouse cursor when mouse is within HTML5 canvas with JavaScript?

Sometimes, we want to hide mouse cursor when mouse is within HTML5 canvas with JavaScript.

In this article, we’ll look at how to hide mouse cursor when mouse is within HTML5 canvas with JavaScript.

How to hide mouse cursor when mouse is within HTML5 canvas with JavaScript?

To hide mouse cursor when mouse is within HTML5 canvas with JavaScript, we can set the cursor CSS property to 'none'.

For instance, we write:

<canvas width='200' height='200'></canvas>

to add a canvas.

Then we write:

document.querySelector('canvas').style.cursor = "none";

to select a canvas with querySelector.

And then we set the canvas’ style.cursor to 'none' to hide the cursor when the mouse is in the canvas.

Conclusion

To hide mouse cursor when mouse is within HTML5 canvas with JavaScript, we can set the cursor CSS property to 'none'.

Categories
JavaScript Answers

How to map an array with duplicate values to a unique array in JavaScript?

Sometimes, we want to map an array with duplicate values to a unique array in JavaScript.

In this article, we’ll look at how to map an array with duplicate values to a unique array in JavaScript.

How to map an array with duplicate values to a unique array in JavaScript?

To map an array with duplicate values to a unique array in JavaScript, we can use some array methods.

For instance, we write:

const data = [{
    "topicId": 1,
    "subTopicId": 1,
    "topicName": "a",
    "subTopicName1": "w"
  },
  {
    "topicId": 2,
    "subTopicId": 2,
    "topicName": "b",
    "subTopicName2": "x"
  },
  {
    "topicId": 3,
    "subTopicId": 3,
    "topicName": "c",
    "subTopicName3": "y"
  },
  {
    "topicId": 1,
    "subTopicId": 4,
    "topicName": "c",
    "subTopicName4": "z"
  }
];

const noDups = [...new Set(data.map(d => d.topicId))].map(topicId => {
  return data.find(d => d.topicId === topicId)
})
console.log(noDups)

We create a new set with the topicIds with the duplicates removed and spread that back to an array.

Then we call map with a callback to return the first item with the given topicId in data.

Therefore, noDups is:

[
  {
    "topicId": 1,
    "subTopicId": 1,
    "topicName": "a",
    "subTopicName1": "w"
  },
  {
    "topicId": 2,
    "subTopicId": 2,
    "topicName": "b",
    "subTopicName2": "x"
  },
  {
    "topicId": 3,
    "subTopicId": 3,
    "topicName": "c",
    "subTopicName3": "y"
  }
]

Conclusion

To map an array with duplicate values to a unique array in JavaScript, we can use some array methods.

Categories
JavaScript Answers

How to detect text change with Mutation Observer and JavaScript?

Sometimes, we want to detect text change with Mutation Observer and JavaScript?.

In this article, we’ll look at how to detect text change with Mutation Observer and JavaScript.

How to detect text change with Mutation Observer and JavaScript?

To detect text change with Mutation Observer and JavaScript, we can set the characterData option to true.

For instance, we write:

<div>

</div>

to add a div.

Then we write:

const mutate = (mutations) => {
  mutations.forEach((mutation) => {
    console.log(mutation);
  });
}

const target = document.querySelector('div')
const observer = new MutationObserver(mutate);
const config = {
  characterData: true,
  attributes: false,
  childList: true,
  subtree: false
};

observer.observe(target, config);

setTimeout(() => {
  target.textContent = 'hello world';
}, 1000);

We create a MutationObserver instance with the mutate function as the callback.

And we call observe with a config that has characterData set to true to observe text node changes.

Therefore, when we change the textContent of target to 'hello world', mutate would run.

Conclusion

To detect text change with Mutation Observer and JavaScript, we can set the characterData option to true.

Categories
JavaScript Answers

How to get child element from event.target with JavaScript?

Sometimes, we want to get child element from event.target with JavaScript.

In this article, we’ll look at how to get child element from event.target with JavaScript.

How to get child element from event.target with JavaScript?

To get child element from event.target with JavaScript, we can call querySelector on event.target.

For instance, we write:

<form>
  <div>
    <input />
  </div>
  <button type="submit">Submit</button>
</form>

to add a form with an input.

Then we write:

const form = document.querySelector('form')
form.onsubmit = (event) => {
  event.preventDefault()
  console.log(event.target.querySelector('input'));
}

to select the form with querySelector.

Next, we set form.onsubmit to a function that gets the input from the form with event.target.querySelector('input').

Since the submit event is triggered by the form, event.target is the form.

Conclusion

To get child element from event.target with JavaScript, we can call querySelector on event.target.