You are binding the event handler inline in HTML also you are using jQuery to bind again inside the function which is not correct.
Just remove the inline onclick,
<input type="button" id="ImageHosting" value="To Image Hosting" />
And change JS
$(document).ready ( function () {
$("#ImageHosting").click(function () {
alert("test");
});
});
Incase if this button is inserted dynamically then,
$(document).ready ( function () {
//replace document below with enclosing container but below will work too
$(document).on('click', "#ImageHosting", function () {
alert("test");
});
});
Use .live/.delegate if you older version of jQuery ( < 1.7)
You are binding the event handler inline in HTML also you are using jQuery to bind again inside the function which is not correct.
Just remove the inline onclick,
<input type="button" id="ImageHosting" value="To Image Hosting" />
And change JS
$(document).ready ( function () {
$("#ImageHosting").click(function () {
alert("test");
});
});
Incase if this button is inserted dynamically then,
$(document).ready ( function () {
//replace document below with enclosing container but below will work too
$(document).on('click', "#ImageHosting", function () {
alert("test");
});
});
Use .live/.delegate if you older version of jQuery ( < 1.7)
Conversely to SKS's answer (keeping the inline onclick attribute):
<input type="button" id="ImageHosting" value="To Image Hosting" onclick="ImageHosting_Click()"/>
And
function ImageHosting_Click(){
alert("test");
}
Or even all in one:
<input type="button" id="ImageHosting" value="To Image Hosting" onclick="alert('test');"/>
Try something like this:
function myfunction() {
var value = document.getElementById("textfield").value;
document.getElementById('messageID').value=value;
alert(value);
}
<input type="button" value="Alert" onclick="myfunction()" type="button" class="btn btn-danger" id="button"/>
<textarea id="textfield">Hello World</textarea>
<input id="messageID">
Three things I'm seeing wrong:
.val(value);is a jQuery' method, not javascript... you should change it to.value = value;to call
onclick="myfunction()"you should name it:var myfunction = function(){The
document.getElementById()method doesn't need sharp#before the name.
Hope it helps.
The alert() function is synchronous and you can't verify what was clicked (it does not return anything), so the code below the call will be executed after it is closed (ok or close button). The alert is not used to gain user input. It is an alert, a message to the user. If you need to check what the user want, you should use confirm(). Note that the function name tells its purpose like alert.
Something like:
// if the ok button is clicked, result will be true (boolean)
var result = confirm( "Do you want to do this?" );
if ( result ) {
// the user clicked ok
} else {
// the user clicked cancel or closed the confirm dialog.
}
Alert is a blocking function, means, if you don't close it, the code below will not execute. So you don't have to capture the alert close event, just write down the code below that alert, when alert window will be closed the code below will be executed automatically.
See example below:
alert("Close Me");
// Write down the code here, which will executed only after the alert close
console.log("This code is executed after alert")
document.getElementsByClassName("addbutton") returns an Array(HTMLCollection to be precise).
Also, you need to set the onclick to a function, you cannot use the syntax .onclick { }.
To set a listener for every button you will need to use a for loop somewhat like this
for(let i = 0; i < addbuttons.length; i++){
addbuttons[i].onclick = function(){
alert('Works!')
}
}
Or if you are using ES6
for(let addButton of addbuttons){
addButton.onclick = function(){
alert('Works!')
}
}
Probably relevant links,
https://www.w3schools.com/js/js_loop_for.asp
https://www.w3schools.com/jsref/event_onclick.asp
Let's define your event:
function myEvent() {
alert("hi");
}
Now let's get your elements by class name, loop them and add an event listener to them:
for (let btn of document.getElementsByClassName("addbutton")) btn.addEventListener("click", myEvent);
Your mistake was that you assumed that you have an onclick for the set of elements, returned by getElementsByClassName, however, you only have onclick for the elements inside the set.
JavaScript is single-threaded, which means when you call a function, it blocks until it returns. When you call alert(), that passes control to the browser which decides how to handle it. It is not Javascript which is popping the UI dialog, it is the browser. alert() does not return until the browser receives the "OK" event and returns control. The javascript thread is halted until that happens.
So for at least two different reasons stated in the above paragraph, the answer is no :)
I'm pretty sure it's not possible.
Alternatively, you can opt to not load an entire Javascript library for something as simple as this:
document.querySelector('#button').addEventListener('onclick', exit_alert);
You can do this with jQuery
$( "#targetId" ).click(function() {
alert( "Handler for .click() called." );
});