SitePoint Sponsor |
|
User Tag List
Results 1 to 14 of 14
Hybrid View
-
Jul 19, 2007, 17:10 #1
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Can anyone help me with this small JS function please?
Code JavaScript:<script type="text/javascript"><!-- if (document.getElementById("deletethisfile") || document.getElementById("deletethisfile") != null || document.getElementById("deletethisfile") != "undefined") { function ondeleteclick() { if (document.getElementById('deletethisfile').checked == true) { return window.confirm("Are you sure to delete this?"); } } } else if (!document.getElementById("deletethisfile") || document.getElementById("deletethisfile") == null || document.getElementById("deletethisfile") == "undefined") { function ondeleteclick() { return true; } } //--></script>
And then I'm calling onsubmit="ondeleteclick()" with the opening form tag.
The idea is, if someone has checked the chekbox deletethisfile before hitting the submit, he gets a confirmation box. It's not working, I mean, no confirmation box is coming on submit even when the check box is selected.
I appreciate your time.
Thanks.
-
Jul 19, 2007, 17:35 #2
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
You're declaring the function but not calling it... also you're declaring the same function name twice.
Code Javascript:function ondeleteclick() { var del = document.getElementById("deletethisfile"); if (!del) return; if (del.checked) { return window.confirm("Are you sure to delete this?"); } }
-
Jul 20, 2007, 03:10 #3
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Raffles, it's again you.
Thanks, this one is much compact compared to whay I tried. And here, I'm getting the confirm box. However, even if I click on cancel, it's returning true.
Any guess?
-
Jul 20, 2007, 04:33 #4
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
It can't be. I've tried it with a simple inline event handler:
HTML Code:<p onclick="alert(ondeleteclick())">Hi</p>
-
Jul 20, 2007, 05:29 #5
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
yeah, I know it sounds strange ... but
Code JavaScript:<script type="text/javascript"><!-- function ondeleteclick() { var del = document.getElementById("deletethisfile"); if (!del) return; if (del.checked) { return window.confirm("<?=DELETE_CONFIRM_IMAGE?>"); } } //--></script>
is all what I'm using.
The form call is -
Code HTML4Strict:<form action="manage-post.php" id="add" enctype="multipart/form-data" method="post" onsubmit="ondeleteclick()">
and then finally
Code HTML4Strict:<input type="checkbox" name="deletethisfile" id="deletethisfile" value="dtf" />
What can be wrong ? Do you see anything ?
-
Jul 20, 2007, 06:36 #6
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Ah yes, duh. It should be onsubmit="return ondeleteclick()".
Why don't you put the event handler in an external file instead of inline?
-
Jul 20, 2007, 06:45 #7
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jul 20, 2007, 06:44 #8
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ah ... thanks Raffles ...
thanks a lot.
-
Jul 20, 2007, 06:55 #9
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
No, I mean that you don't need to have onsubmit="..." in the HTML. That is called an inline event handler, the same as onclick="..." and onmouseover="...".
Putting it outside, in an external javascript file, is good practice. For example, in your case you could replace your ondeleteclick function with this::
Code:document.getElementById('add').onsubmit = function() { var del = document.getElementById("hi"); if (!del) return; if (del.checked) { return window.confirm("Are you sure to delete this?"); } }
Google "unobtrusive javascript" and you'll find a wealth of articles on the matter.
-
Jul 20, 2007, 07:05 #10
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ah, it's great, and of course I will like this solution. But what if I have two forms with id add ? Basically, whenever I am making a form, I am making two copies of it, one in the first page, and second that comes in case any of the mandatory field is left empty. How you do this?
-
Jul 20, 2007, 07:21 #11
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
If you have two forms with the id "add", you've done something else wrong. The ID attribute is unique, that is, no two elements can have the same ID. The obvious solution is to have a different ID for the second one.
-
Jul 20, 2007, 07:23 #12
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ok ... thanks Raffael, that will mean, I will call the same function twice, each time for each id ... hmmm ...
ok, thanks a lot Raffael
-
Jul 20, 2007, 08:49 #13
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Yeah, but that's why you give the function a name and then call it, you don't have to copy the entire thing out. You probably need to wrap them up in a window.onload function too, in case you haven't got one of them already:
Code javascript:window.onload = function() { document.getElementById('add1').onsubmit = ondeleteclick; document.getElementById('add2').onsubmit = ondeleteclick; } function ondeleteclick() { var del = document.getElementById("hi"); if (!del) return; if (del.checked) { return window.confirm("Are you sure to delete this?"); } }
-
Jul 20, 2007, 10:41 #14
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That's great Raffael ... thanks a lot once again ...
Bookmarks