Welcome to WebmasterWorld Guest from 54.162.248.199

Forum Moderators: incrediBILL

Message Too Old, No Replies

If checkbox checked problem

     
4:09 pm on Sep 21, 2010 (gmt 0)

5+ Year Member



NOT SURE WHAT CATEGORY THIS FALLS UNDER?

I am using the following piece of code to open a new window when a checkbox is checked...

<INPUT type="checkbox" name="name" value="YES" onClick="window.open('/somepage.php?do=something&id=<?php echo $SOMEID["id"] ?>','','width=600,height=350,left=100,top=100,screenX=100,screenY=100')">

The page opens ok when the checkbox is ticked (checked).

PROBLEM:
The page opens again if the checkbox is un-ticked (un-checked).

I just want to open the page if the checkbox is ticked only but not sure how to do this?

OTHER INFO:
Seperate checkboxes are dynamically created on the page for each id=<?php echo $SOMEID["id"] ?>
8:11 pm on Sep 21, 2010 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



onClick="if(this.checked) {window.open('/somepage.php?do=something&id=<?php echo $SOMEID["id"] ?>','','width=600,height=350,left=100,top=100,screenX=100,screenY=100');}">
9:16 pm on Sep 21, 2010 (gmt 0)

5+ Year Member



Fotiman you are the 'Man'!

A million thanks!
9:26 pm on Sep 21, 2010 (gmt 0)

5+ Year Member



Dear Fotiman - just a thought but would it be possible to close the same window that was opened using what you supplied above only using..

if (this.unchecked) {window.close etc ?

Something like this...

<INPUT type="checkbox" name="name" value="YES" onClick="if(this.checked) {window.open('/somepage.php?do=something&id=<?php echo $SOMEID["id"] ?>','','width=600,height=350,left=100,top=100,screenX=100,screenY=100');} if (this.unchecked) {window.close('/somepage.php?do=something&id=<?php echo $SOMEID["id"] ?>');}">
1:45 pm on Sep 22, 2010 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



onClick="if(this.checked) {w = window.open('/somepage.php?do=something&id=<?php echo $SOMEID["id"] ?>','','width=600,height=350,left=100,top=100,screenX=100,screenY=100');}else{w.close();}">

Ideally, you should probably not define this script inline, as it will be harder to maintain and less efficient. You might do something like this instead:

<script type="text/javascript">
var WindowMgr = {
_w: {},
checkboxHandler: function (el, id) {
var wId = 'window' + id,
w = this._w[wId];
if (el.checked) {
w = window.open('/somepage.php?do=something&id=' + id,
wId,
'width=600,height=350,left=100,top=100,screenX=100,screenY=100');
}
else {
if (w) {
w.close();
}
}
this._w[wId] = w;
}
};
</script>
<INPUT type="checkbox" name="name" value="YES" onclick="WindowMgr.checkboxHandler(this,<?php echo $SOMEID["id"]?>);">
<INPUT type="checkbox" name="name" value="YES" onClick="WindowMgr.checkboxHandler(this,<?php echo $SOMEID["id"]?>);">
1:49 pm on Sep 22, 2010 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Note, the first method I gave above will cause the same window to be opened/closed regardless of what checkbox was checked, so if user checks checkbox 1, window opens, then user checks checkbox 2, and the first window is replaced by the window for the 2nd checkbox, then user unchecks checkbox 1 and the window opened for checkbox 2 will go away, so if user then unchecks checkbox 2, there will be a javascript error because w is not defined. The second method I posted will create a new window for each checkbox, and unchecking the first checkbox would close only the window that was opened by the first checkbox.
2:02 am on Sep 24, 2010 (gmt 0)

5+ Year Member



That's brilliant Fotiman!

I'm sure this will help other too as I have come across others with similar problem trawling the internet.

many thanks!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month