I'd like to disable a javascript function (dialogs()) using jQuery.
What I thought to do was:
- Wrap the function in a span:
"<span class = 'auto'>" + dialogs(i,0); + "</span>" If the checkbox is checked, do: (this
ifstatement is in$(document).ready(function(){)if ($("#autodialog").is(":checked")) { $(".auto").remove(); }
But this doesn't seem to be working.
Any thoughts?
-
1@Ben What is confusing you? I want to allow users to decide whether a certain javascript function is run... so I'm giving them a checkbox... when it's checked, it should comment out the function from running (function is a recursive function within a function)user3871– user38712013年08月23日 04:42:35 +00:00Commented Aug 23, 2013 at 4:42
-
I guess the whole "disabling a javascript function". nvmBen– Ben2013年08月23日 04:45:34 +00:00Commented Aug 23, 2013 at 4:45
3 Answers 3
Make your dialogs methods to handle a extra parameter isEnabled, a boolean dataType.
function dialogs(i,0,isEnabled) {
if(isEnabled) {
//Todos
}
}
then make it to look like this
if ($("#autodialog").is(":checked")) {
dialogs('i',0,false); //I'm not sure about the values of parameter i
}
So there is no need of using span tag as a wrapper.
Hope you understand.
1 Comment
A little more context would be helpful (do you have a jsfiddle we can see?)
I think you may be confusing the Javascript function and the value returned from the function. Are you trying to remove a string of HTML generated by the dialogs() function, or are you trying to remove the actual dialogs function itself?
If you want to disable the dialogs function:
<script>
function getDialogs(a,b) {
// ...
}
var dialogs = getDialogs; // Make dialogs refer to getDialogs
</script>
Elsewhere you'll have something like:
<script>
if ( $("#autodialog").is(":checked") ) {
dialogs = function __noop__() {};
} else {
dialogs = getDialogs;
}
</script>
Comments
you will have to add a click handler to the checkbox
$("#autodialog").click(function(){
if ($(this).is(":checked")) {
dialogs(i,no,false); }
else
dialogs(i,no,true);
});
function dialogs(i,no,flag){
if(!flag)
{event.preventDefault();return flag;}
else{
......//your working code}
}
6 Comments
document.ready to run through that if statement without a handlerspan tag inorder to disable the dialogs method.