Skip to content

Instantly share code, notes, and snippets.

@serdarwork
serdarwork / target_blank.js
Created March 17, 2021 12:34
JAVASCRIPT: Add target="_blank" to links within specificied div(s)
// Specify div(s)
const fieldNames = ['.more-link', '#featured-posts a', '.page-link a'];
// Select div(s)
const selectFields = fieldNames.map((field) => document.querySelectorAll(field));
selectFields.forEach((e, i) => {
if (selectFields[i].length > 0) {
// Get links within a specificied div
const linkArray = Array.prototype.slice.call(e);