0

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.

4
  • 1
    Can't see the documentation say anywhere that you could toggle this dynamically, after the widget has already been rendered. Commented Aug 28, 2023 at 13:25
  • "I tried directly" - a mere verbal description is not enough for us to be able to tell what might have gone wrong; show code when you talk about something you tried. Commented Aug 28, 2023 at 13:27
  • @CBroe You are absolutely correct. I never thought that being able to dynamically set something would ever be a question, but I guess it was. Commented Aug 28, 2023 at 14:14
  • @CBroe I think you're over thinking it. Don't pay attention to the word "directly", just understand that I grabbed all the result titles by their class name and tried to use setAttribute() on them. Commented Aug 28, 2023 at 16:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.