0

I would like to find:

<input type="button" class=" btn btn-lg btn-info" value="Continue" onclick="loadFinal();">

and replace it with:

<input type="button" class=" btn btn-lg btn-info" value="Continue" onclick="loadFinal(); applyCode();">.

In short, insert applyCode(); to onclick.

2 Answers 2

2

There are 2 things you can do. Approach 1 would remove all click events from the button and add events, approach 2 would add additional events in addition to the existing event.

Approach 1

$('input:button').unbind('click'); // remove all click events associated
$('input:button').on("click",function() // add new click events
{
  loadFinal();
applyCode();
});

Approach 2

$('input:button').on("click",function() // add additional click events
    {
    applyCode();
    });

UPDATE :

In order to apply or append the applycode() event to the same element that has loadfinal(), you can use $._data to extract the events and check if a certain event exists or not.

$("input:button").click(function() {
    var events = $._data(document.getElementById('btn1'), "events");
    $.each(events,function(i,o)
    {
        alert(i);// give the event type like 'click'
        alert(o); // give the event object - you can extract the event name from the object and do the compare with the existint event.
    });
});

Alternately, you can directly target the click event.

var events = $._data(document.getElementById('btn1'), "events").click;
        $.each(events,function(i,o)
        {
            alert(i);// give the index 
            alert(o); // give the event object - you can extract the event name from the object and do the compare with the existint event.
        });
    });

Implementation : http://jsfiddle.net/qk5qj33u/15/

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

7 Comments

These solutions works! Thanks. But as mentioned on the post beneath, I would need an ID or some way targeting that input only, since that is not the only input tag on the document. Could it be targeted by using load final(); since it's unique on the document.
I've updated the solution. Please follow the section marked with UPDATE.
I see you are getting the element by ID, "btn1". But, I can't add that ID to the form. I don't have access to the source code. Can this be done using get Element By the onclick value?
Unfortunately that's the only native solution that works with jquery 1.10+. If that doesn't workout for you, you may wanna look into other API's such as github.com/sebastien-p/jquery.hasEventListener
OK. Understood. I used instead the Approach 2 of your first solution. Which in turn applies: applyCode(); to all <input>, which is ok. Thanks! Right?
|
0

I don't think find & replace is what you'd be best using. Instead you can use Javascript to attach events for you. As you've tagged the question with jQuery, try this:

<input type="button" class=" btn btn-lg btn-info" value="Continue"> 

$(function() {
    $('input[type=button]').click(function() {
        loadFinal();
        applyCode();
    });
});

Note that the above selector is purely for demonstration purposes, you'd be better setting an id attribute, or single specific class on the element and selecting it by that instead. You could even hook to the submit event of the form, assuming this button is used for that purpose.

2 Comments

This solution works! Thanks! But, as you mention, it needs an ID. I can't add an ID because the code is already given and can't be modified. Is there a way to get element by attribute? the value loadFina(); is unique on the whole document.
OK. Understood. I deleted from your code: load final();, which in turn applies: applyCode(); to all <input>, which is ok. Thanks! Right?

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.