samedi 2 juillet 2016

jQuery/JavaScript - get clicked button itself in a function


I need a way to get the element pressed in a function called by that element, and disable it.

I tried using $(this), but is not working, this is my code:

<script>
function x(){
    $(this).prop("disabled",true);
}
<script>
<input type='button' onclick='x()' value='Disable me' />
<input type='button' onclick='x()' value='Disable me' />

    function x(){
        $(this).prop("disabled",true);
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='button' onclick='x()' value='Disable me' />
<input type='button' onclick='x()' value='Disable me' />

I want to avoid add and call an id, is there any way to do it?

Thank you!

-- edit --

  • I can not edit the input elements, they are in another file.
  • the JS code is called before the html file, and I can not edit the html file.

Aucun commentaire:

Enregistrer un commentaire