Travis Travis - 1 year ago 92
Javascript Question

Opening link in new tab if Check box is checked Otherwise opening it on same page

<!DOCTYPE html>
<title> Untitled </title>
<script type="text/javascript">

function OpenWindow(info) {
<input type="checkbox" name="CBox0" value="" > Click To Open Link in New Window!</p>


<a href="" target="new" id="y"onclick="if(this.checked){OpenWindow(this.value)}">Youtube</a>


This is what i have so far. When the check box is checked, if i click on a link, it will open the website up in a new tab. But if it is not checked, it opens up on the same page like a regular link. I have been working on this for awhile, so any help will be greatly appreciated. Thanks!

Answer Source

You had some markup issues and inline stuff that I've removed. This should do it for you:

<input id="checky" type="checkbox">Click To Open Link in New Window</input>
<br />
<a href="" id="linky">Youtube</a>

var checky = document.getElementById('checky');
var anchor = document.getElementById('linky');
var link = anchor.getAttribute('href');

function OpenWindow(href) {
    if (checky.checked) {, '_blank');
    } else {, '_self');
anchor.onclick = function () {
    return false;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download