Run jQuery on button onclick

2

How to call jQuery directly in the onclick of button? , for example:

<input type="button" id="btnteste" onclick="$("#IDdoTextBox").spectrum("set", $("IDdoCampo").val());" />

And it does not work, it does not return error or anything. In fact, in Visual Studio itself it is already in red as the wrong code.

The way below works, however as I said I do not want, since everything will be created dynamically in ASP.NET

<script>
$("#btnteste").click(function() {
    $("#IDdoTextBox").spectrum("set", $("#IDdoCampo").val());
});

    
asked by anonymous 02.04.2014 / 22:10

3 answers

1

You are breaking HTML syntax by using "(double quotes) within the value of onclick, try to use '(single quotation marks).

Change from:

<input type="button" id="btnteste" onclick="$("#IDdoTextBox").spectrum("set", $("IDdoCampo").val());" />

To:

<input type="button" id="btnteste" onclick="$('#IDdoTextBox').spectrum('set', $('IDdoCampo').val());" />
    
02.04.2014 / 22:18
1

The best thing to do is stay by the script, more about it can be seen here link

But if you want the content to have the onclick then try

<input type="button" id="btnteste" onclick="$('#IDdoTextBox').spectrum('set', $('#IDdoCampo').val());" />
    
02.04.2014 / 22:37
0

It may be that the quotation marks try to use single quotation marks like this:

<input type="button" id="btnteste" onclick="$('#IDdoTextBox').spectrum('set', $('IDdoCampo').val());" />
    
02.04.2014 / 22:17