Elvira
Elvira

Reputation: 1440

javascript window.open and adding parameters

I'm having some issues with my javascript link

I have a hidden input fields with the name "domein"

I'm having the following div with a onclick script

<div id="button" onclick="window.open('http://www.mylink.nl/?domein=' + document.getElementById('domein').value ,'_blank') ">
<strong>button text</strong>
</div>

The link does not open in a new tab neither does it shows the values from the 'domein' value from the hidden field in the url.

Can you guys please help me out?

My second question: How can I let the url be only http://www.mylink.nl when 'domein' value = empty.

Upvotes: 0

Views: 8497

Answers (1)

brso05
brso05

Reputation: 13222

This works:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<input type="hidden" id="test" value="test">
<div id="button" onclick="window.open('http://www.example.com/?domein=' + document.getElementById('test').value,'_blank');">
<strong>button text</strong>
</div>
</body>
</html>

New tab url:

http://www.example.com/?domein=test

Please check and make sure you have your hidden input defined with id domein...

******************************UPDATE********************************

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
function submit()
{
    if(!document.getElementById("test").value.match(/\S/))
    {
        window.open('http://www.example.com/','_blank');
    }
    else
    {
        window.open('http://www.example.com/?domein=' + document.getElementById('test').value,'_blank');
    }
}
</script>
<input type="hidden" id="test" value="test">
<div id="button" onclick="submit()">
<strong>button text</strong>
</div>
</body>
</html>

Upvotes: 1

Related Questions