0

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 if statement is in $(document).ready(function(){)

    if ($("#autodialog").is(":checked")) {
       $(".auto").remove(); }
    

But this doesn't seem to be working.

Any thoughts?

2
  • 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) Commented Aug 23, 2013 at 4:42
  • I guess the whole "disabling a javascript function". nvm Commented Aug 23, 2013 at 4:45

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Growler I'm glad I was able to help.
1

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

0

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

Thought I could just allow the document.ready to run through that if statement without a handler
@Growler does you checkbox comes checked when the docuement load? no right?! so that if condition won't be true
Also, can I just disable the function somehow without removing it? If the user unchecks the box, I'd like the function to run again... so I don't thin removing it would be good
@Growler the updated code allows you to rewrite the function call inside the span, the function takes a boolean flag which if true will execute your function else will return false
You're making it so complicated. OP is using span tag inorder to disable the dialogs method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.