I'm pretty new to JS and jQuery
, and i'm trying to make a subtitles player using them. Unfortunately, i'm stuck in a very early stage.
When I'm trying to select some HTML
elements through a .js file, it acts like it can't locate the element I'm asking for, and nothing happens. If I try to alert the value or the HTML of the elements, it alerts undefined
.
So this is the code:
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="jquery-2.1.3.min.js"></script>
<script src="script.js"></script>
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
#wrapper{
width: 150px;
text-align: center;
background: -webkit-gradient(linear, left top, right bottom, color-stop(0%,#75bdd1), color-stop(14%,#75bdd1), color-stop(100%,#2294b3));
padding: 10px 2px;
}
h3{
font-family: Arial, Helvetica, sans-serif;
}
img{
width: 50px;
margin: 0 auto;
}
input{
display: none;
}
</style>
</head>
<body>
<div id="wrapper">
<h3>Select subtitles file</h3>
<img src="browse.png" alt="browse">
</div>
<input type="file" accept=".srt" id="file">
</body>
</html>
script.js
$("div").click(function () {
console.log('loaded');
});
Thanks.
JavaScript is the Programming Language for the Web. JavaScript can update and change both HTML and CSS. JavaScript can calculate, manipulate and validate data.
Hands down, JavaScript is undeniably better than Python for website development for one simple reason: JS runs in the browser while Python is a backend server-side language. While Python can be used in part to create a website, it can't be used alone.
The answer: JavaScript is more difficult to master than Python. Python is usually the beginners-choice, especially for those who do not have any prior programming experience. Python code is notorious for being more readable, meaning that it is easier to understand (and write).
While Java necessitates use of objects throughout the codebase, JavaScript is considerably more forgiving, allowing for simple linear programming without the use of objects. Both languages allow for inheritance and polymorphism – the main staples of Object Oriented Design.
Because your script
tag is above the HTML defining the elements that it acts on, it can't find them because they don't exist as of when that code runs. Here's the order in which things are happening in your page:
html
and head
elements are createdmeta
element is created and its content noted by the parserscript
element for jQuery is createdscript
element for your code is createddiv
elements yetWays to correct it:
Move the script
elements to the very end, just before the closing </body>
tag, so all of the elements exist before your code runs. Barring a good reason not to do this, this is usually the best solution.
Use jQuery's ready
feature.
Use the defer
attribute on the script element, but beware that not all browsers support it yet.
But again, if you control where the script elements go, #1 is usually your best bet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With