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');"/>
No, this is, as you say "rubbish code". If it works as should, it is because browsers try to "read the writer's mind" - in other words, they have algorithms to try to make sense of "rubbish code", guess at the probable intent and internally change it into something that actually makes sense.
In other words, your code only works by accident, and probably not in all browsers.
Is this what you're trying to do?
<a href="#" onClick="alert('Hello World!')"><img title="The Link" /></a>
When you click on the image you'll get the alert:
<img src="logo1.jpg" onClick='alert("Hello World!")'/>
if this is what you want.
Imagine you have a method like so:
function getString(){
return "string";
}
and then define a textbox:
<input type="text" value={getString()}/>
You would expect that the textbox would be rendered with a value of "string" not "getString()". This is because what is within the curly braces is evaluated in the render method.
When {alert('hi)} is evaluated it runs the method so what is assigned to the onclick event is what is returned by alert('hi') (nothing) not the method itself.
In react application, we write html in javascript and not in HTML.
So at the time of rendering, javascript will execute this function: alert('hi')
By using this syntax:
<button onClick={() => alert('hi')}>Click me!</button>
we are passing function reference to on click. Inner block of this function will only be executed when this event is called.
I never really understand why someone wants to do it this way, but here is an inline alert on a button.
<button type="button" onclick="alert('Hello World')">Show Alert</button>
Here is the fiddle
By the way, I believe this question has been answered
Did I answer your question?
<input type='button' onclick="javascript:alert('test');" value='Button'/>
You can also use the following codes:
<a href="#" id="link">Link</a>
Javascript:
$(document).on("click","#link",function(){
alert("I am a pop up ! ");
});
OR:
<a href="#" onclick="alert('I am a popup!');">Link</a>
Without using a JS file (purely as an alternative to answers already posted):
<a href="http://example.com" onclick="alert('Hello world!')">Link Text</a>
You're forgetting the quotes on your parameter. Hope this helps
function alert_phrase(id){
var value = document.getElementById(id).value;
alert(value);
}
<div id="exp" style="background-color: white;">
<p id="content-exp" style="color: red;">HELLO WORLD!</p>
<input name="phrase" Placheholder="Enter text here" id="phrase" />
<button onclick='alert_phrase("phrase")'>Alert this sentence</button>
Try this ...
<button onclick='alert_phrase("phrase")'>Alert this sentence</button>
Note: passing the id as a string to alert_phrase.
Snippet:
function alert_phrase(id){
var value = document.getElementById(id).value;
alert(value);
}
<div id="exp" style="background-color: white;">
<p id="content-exp" style="color: red;">HELLO WORLD!</p>
<input name="phrase" Placheholder="Enter text here" id="phrase" />
<button onclick='alert_phrase("phrase")'>Alert this sentence</button>
</div>