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)
Videos
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');"/>
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." );
});
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.
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.
here you have what you needed (http://jsfiddle.net/2v2C2/2/)
// when one of buttons clicked this func will execute
function buttonClick (e) {
var input = e.target;
// alert different message for different buttons
switch(input.getAttribute('id')) {
case "english":
alert("Hello! How are you?");
break;
case "spanish":
alert("Hola! Como estas?");
break;
case "hebrew":
alert("Shalom!");
break;
case "frech":
alert("Bonjour!");
break;
default:
alert("Unexpected button pressed ...i don't know what to say");
}
}
//store buttons in variable buttonsList
var buttonsList = document.getElementsByTagName("input");
//iterate through all elements of buttonList and attach event handler if they are buttons (not all input elements are buttons)
for (var i = 0; i<buttonsList.length; i++) {
var input=buttonsList[i];
if (input.getAttribute('type')=='button') {
input.onclick = buttonClick;
}
}
I saw your problem and found a simple solution for you as you are new in programming.So Please read care fully you will understand everything.As below example,you want to get which button is clicked and according to that you want to alert different message.You can do this easily with jQuery.
See your desired result here http://jsfiddle.net/q6QLy/26/
<body>
<ul style="list-style-type:none" id="buttons">
<li>
<input type="button" value="English" id="english" />
</li>
<li>
<input type="button" value="Spanish" id="spanish" />
</li>
<li>
<input type="button" value="Hebrew" id="hebrew" />
</li>
<li>
<input type="button" value="French" id="frech" />
</li>
</ul>
</body>
$(document).ready(function() {
$('input:button').click(function() {
var buttons = $(this).val();
alert("You have clicked on button "+buttons);
switch(buttons){
case "English":
alert("Hello ,how are you ?");
break;
case "Spanish":
alert("Hola! Como estas?");
break;
case "Hebrew":
alert("Shalom!");
break;
case "French":
alert("Bonjour!");
break;
default:
alert("Please select a language");
}
});
});
and also you want to get clicked input type is button then you can use same
var buttonsList = document.getElementsByTagName("input");
for (var i = 0; i<buttonsList.length; i++) {
var input=buttonsList[i];
if (input.getAttribute('type')=='button') {
//write your function whatever you want.
}
}