I am trying to accomplish the simple task of giving the user a checkbox in which they are able to set the link target for the results on a Google Custom Search "widget" (if you will). But for some reason, the appropriate attribute that I am required to set for the search "widget" to change this according to https://developers.google.com/custom-search/docs/element#supported_attributes is not doing anything at all. See for yourself below.
var ntChk = document.getElementById('ntChk')
ntChk.onclick = function () {
if (ntChk.checked == true) {
document.getElementById('searchresults').setAttribute('data-linkTarget', '_blank');
} else {
document.getElementById('searchresults').setAttribute('data-linkTarget', '_self');
}
}
<!DOCTYPE html>
<html>
<head>
<title>Google Custom Search Example</title>
</head>
<body>
<input type="checkbox" id="ntChk" /> Open results in new tab
<script async src="https://cse.google.com/cse.js?cx=ea0629e3e0cd64c16"></script>
<div class="gcse-search" id="searchresults"></div>
</body>
</html>
I tried directly setting the target by grabbing the class of all the result titles and using setAttribute('', '') on them, but then my browser gave me an error saying that this operation was not allowed. So, I tried using the .target = "" method but this produced no result at all (not even visually through Inspect Element).
Any assistance will be greatly appreciated.