I need to like to split button in HTML file and it's click javascript function into javascript file.
Here is what I have tried so far:
Trial 1: Combine HTML submitbutton button and button's click function
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
</head>
<body>
<form id="advertiseForm" name="advertiseForm" method="post" >
<br /><br /><br />
<div style="text-align:center">
<input class="button button2" id="submitbutton" type="button" value="Submit" />
</div>
<script>
$( "#submitbutton" ).on( "click", validatelanded );
function validatelanded( event ){
alert( "Hello." );
}
</script>
</form>
</body>
Trial 2: Separate HTML sumitbutton button in HTML file and its click function into js file.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script type="text/javascript" src="advertisementlandedvalidationatclient.js"></script>
</head>
<body>
<form id="advertiseForm" name="advertiseForm" method="post" >
<br /><br /><br />
<div style="text-align:center">
<input class="button button2" id="submitbutton" type="button" value="Submit" />
</div>
</form>
</body>
advertisementlandedvalidationatclient.js file
<script type="text/javascript">
$( "#submitbutton" ).on( "click", validatelanded );
function validatelanded( event ){
alert( "Hello." );
}
</script>
Trial 1 works ( I see alert) and Trial 2 does not work (I don't see alert). What could be wrong?
<script>from .js file..<script>is an HTML tag/element/entity, it "doesn't exist" in JavaScript. When you load an external JavaScript fire into an HTML document with a<script>element, the content is parsed as JavaScript, not HTML.