0

I have no experience in js, although i am trying to write a greasemonkey script to duplicate a button in the website. I am using an auction and bid button is in where uncomfortable place, so i am looking to duplicate the button and add it in the bot of the website. So far i spent few hours researching and wrote this script: unfortunately its not working... Any help is appreciated. thank you.

// create button
var btn = document.createElement( 'input' );
with( btn ) {
  setAttribute( 'onclick', ' document.getElementById(\'contentPlaceholder_CurrentSalesList_gvCurrentSales_btnEdit_1').click);
  setAttribute( 'value', 'mygtukas' );
  setAttribute( 'type', 'button' );
}

// append at end
document.getElementsByTagName( 'body' )[ 0 ].appendChild( btn );

The whole project task is: there is a button on the website with id:something_1 and this button moves from time to time, i dont want it to move, so i want to create a duplicate button, in the bottom of the page, so when i click it, it does the same as moving button.

RMmm
  • 11
  • 1
  • @RMmm when you say it moves, what do you mean by this? Are more elements added to the page before it's rendered? Could also create a new button that simply triggers a `click` event on the other button. – Twisty Sep 14 '15 at 17:46
  • its a complete different task am looking to achieve here. its really easy, as i got no experience, and been spending hours on this, am asking for help. – RMmm Sep 14 '15 at 17:48
  • I mean there are different lines, and when someones click on their button, they go to top, when i click i go to top, so the button is next to my name and it moves when i click it. – RMmm Sep 14 '15 at 17:49
  • @Twisty how to trigger click event on the other button? – RMmm Sep 15 '15 at 05:09
  • Posted response as an answer. – Twisty Sep 15 '15 at 05:55

1 Answers1

0

From: How can I trigger a JavaScript event click

Try quick version first:

var btn = document.getElementById("something_1");
btn.click();

Or try more complete version:

/**
 * Fire an event handler to the specified node. Event handlers can detect that the event was fired programatically
 * by testing for a 'synthetic=true' property on the event object
 * @param {HTMLNode} node The node to fire the event handler on.
 * @param {String} eventName The name of the event without the "on" (e.g., "focus")
 */
function fireEvent(node, eventName) {
    // Make sure we use the ownerDocument from the provided node to avoid cross-window problems
    var doc;
    if (node.ownerDocument) {
        doc = node.ownerDocument;
    } else if (node.nodeType == 9){
        // the node may be the document itself, nodeType 9 = DOCUMENT_NODE
        doc = node;
    } else {
        throw new Error("Invalid node passed to fireEvent: " + node.id);
    }

     if (node.dispatchEvent) {
        // Gecko-style approach (now the standard) takes more work
        var eventClass = "";

        // Different events have different event classes.
        // If this switch statement can't map an eventName to an eventClass,
        // the event firing is going to fail.
        switch (eventName) {
            case "click": // Dispatching of 'click' appears to not work correctly in Safari. Use 'mousedown' or 'mouseup' instead.
            case "mousedown":
            case "mouseup":
                eventClass = "MouseEvents";
                break;

            case "focus":
            case "change":
            case "blur":
            case "select":
                eventClass = "HTMLEvents";
                break;

            default:
                throw "fireEvent: Couldn't find an event class for event '" + eventName + "'.";
                break;
        }
        var event = doc.createEvent(eventClass);

        var bubbles = eventName == "change" ? false : true;
        event.initEvent(eventName, bubbles, true); // All events created as bubbling and cancelable.

        event.synthetic = true; // allow detection of synthetic events
        // The second parameter says go ahead with the default action
        node.dispatchEvent(event, true);
    } else  if (node.fireEvent) {
        // IE-old school style
        var event = doc.createEventObject();
        event.synthetic = true; // allow detection of synthetic events
        node.fireEvent("on" + eventName, event);
    }
};

Then execute as follows:

fireEvent(document.getElementById("something_1"), "click");

Full example: http://jsfiddle.net/mendesjuan/rHMCy/4/

Community
  • 1
  • 1
Twisty
  • 30,304
  • 2
  • 26
  • 45