0

Here is the block of code I want to replace:

$(document).ready(function () {    
    $(".button-purple").click(function () {
            interval = $(this).attr('id');
            name = $(this.attr('name');
            if(Number($(this).val()) === 0) {
                if(name == 'static') {
                    do this
                }
                else {
                    do this
                }
            }
            else {
                do this
            }
    });

});

I can't find any documentation on trying to replace the function since it's unnamed though. Is it possible to replace the entire javascript file + delete the line loading it / insert my own script? Would really appreciate any help I can get.

jacob404
  • 3
  • 2
  • Is it possible that you may be going a little to apeshit with all this? Why do you want to `replace` the code block? What are you trying to achieve? – AdityaParab Jan 31 '16 at 10:24
  • I'm trying to make a script to customize a site that I frequent to be more enjoyable for myself. I've already made several customizations, but I need to be able to modify this function in order to achieve what I'm going for. – jacob404 Jan 31 '16 at 10:27
  • @AdityaParab ... tamper**monkey** ... **ape**shit ... funny – Jaromanda X Jan 31 '16 at 12:23
  • @JaromandaX : haha :D you spotted it and related it really well :) – AdityaParab Jan 31 '16 at 12:25

2 Answers2

0

I'm not aware of tampermonkey, but you can try this:

function chickHandler() {
  interval = $(this).attr('id');
  name = $(this.attr('name');
    if (Number($(this).val()) === 0) {
      if (name == 'static') {
        do this
      } else {
        do this
      }
    } else {
      do this
    }
  }
}

function onReadyHandler() {
  $(".button-purple").click(chickHandler);
}

$(document).ready(onReadyHandler);

When you do something like .click(function(){...}), here function is called as a callback. You have to send a function as a callback. Not necessary to be anonymous.

Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • Let me explain a bit better. This is a function within a script on a site that I frequent. I want to locally modify the javascript using an extension called tampermonkey (greasemonkey equivalent) – jacob404 Jan 31 '16 at 10:36
  • So let me rephrase what I understand. There is an external site and you want to modify it locally. Am I right? – Rajesh Jan 31 '16 at 10:52
  • Correct. I've already made some css changes but I'd like to change this script's function to make the site easier for me to use. – jacob404 Jan 31 '16 at 11:01
  • Isn't this unethical? Changing css will change UI not functionality. – Rajesh Jan 31 '16 at 11:02
  • I'm not doing anything malicious, so I don't think it's unethical really. Currently there are buttons which edit a text field (a number) for you, but I wanted to modify the buttons to use the values I frequently use. I could just type these values in the field, but it'd be more convenient to modify the buttons. If that's unethical then let me know, just trying to improve my experience. – jacob404 Jan 31 '16 at 11:27
0

If you just want to remove the click event handler, then simply say

var $element = $(".button-purple");
$element.off('click');

If you want to Remove all the event handlers, then you'll first have to find out what all event handlers are present and then remove them iteratively.

    var element = $element[0]; //Make sure the element is a DOM object and not jQuery Object.

    // Use this line if you're using jQuery 1.8+
    var attachedEvents = $._data(element,'events');

    // Use this line if you're using jQuery < 1.8
    var attachedEvents = $(element).data('events'); //Here you can also replace $(element) with $element as declared above.

    for(var event in attachedEvents){
        $element.off(event);
    }

UPDATE:

You can simply add your own event handler (using .on() API) after you're done removing all the required existing handlers.

Just define your function.

function yourFunction(){ /* your code */};

$element.on('click', yourFunction);

Update 2:

Since you just want to remove the click event handler, this is the simplest code that will serve your purpose.

$(".button-purple").off('click').on('click', yourFunction);
AdityaParab
  • 7,024
  • 3
  • 27
  • 40
  • This is a step in the right direction, but I'd like to add my own event on click. Can I do that after removing the existing event handlers? – jacob404 Jan 31 '16 at 11:02