getelementbyid

How to return value from addEventListener

▼魔方 西西 提交于 2021-02-19 01:39:07
问题 I use a javascript to catch the x and y position when user click a link, by now, I can make it works, but I want it to return the two values to function init() when it is called. How can I do it? <script type="text/javascript"> document.addEventListener("DOMContentLoaded", init, false); function init() { var canvas = document.getElementById("canvas"); canvas.addEventListener("mousedown", getPosition, false); // how can I get the return values here? } function getPosition(event) { var x = new

How to return value from addEventListener

泪湿孤枕 提交于 2021-02-19 01:37:47
问题 I use a javascript to catch the x and y position when user click a link, by now, I can make it works, but I want it to return the two values to function init() when it is called. How can I do it? <script type="text/javascript"> document.addEventListener("DOMContentLoaded", init, false); function init() { var canvas = document.getElementById("canvas"); canvas.addEventListener("mousedown", getPosition, false); // how can I get the return values here? } function getPosition(event) { var x = new

Why use document.getElementById when I can directly refer to the DOM id in JavaScript? [duplicate]

自古美人都是妖i 提交于 2021-02-15 11:39:04
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: IE/Chrome: are DOM tree elements global variables here? I recently discovered that I can use in javascript any object from DOM with a direct reference to its id: <div id="layer">IM A LAYER</div> <script> alert(layer.innerHTML); </script> If this is true, what advantage I'd get using the getElementById method? 回答1: Accessing a DOM element directly will give you a error if the element does not exist. Wheras if you

Why use document.getElementById when I can directly refer to the DOM id in JavaScript? [duplicate]

做~自己de王妃 提交于 2021-02-15 11:36:05
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: IE/Chrome: are DOM tree elements global variables here? I recently discovered that I can use in javascript any object from DOM with a direct reference to its id: <div id="layer">IM A LAYER</div> <script> alert(layer.innerHTML); </script> If this is true, what advantage I'd get using the getElementById method? 回答1: Accessing a DOM element directly will give you a error if the element does not exist. Wheras if you

Why does `document.getElementById(“#sideNav”)` not find my element?

a 夏天 提交于 2021-02-05 12:28:34
问题 Why is this code throwing “ TypeError : Cannot read property ' style ' of null ” ? function openNav() { document.getElementById("#sideNav").style.width = "250px"; } function closeNav() { document.getElementById("#sideNav").style.width = "0"; } <div class="sidenav" id="sideNav"> <a href="javascript:void(0)" class="closenav" onclick="closeNav()">×</a> <a href="#">Home</a> <a href="#">Listing</a> <a href="#">About Us</a> <a href="#">Contact</a> </div> <span onclick="openNav()">OPEN</span> <div

Why isn't getElementsByClassName working?

梦想的初衷 提交于 2021-02-04 21:56:48
问题 I can't understand why getElementsByClassName isn't working here. n = new Date(); y = n.getFullYear(); document.getElementByClassName("date").innerHTML = y; <span class="date"></span> CompanyName 回答1: The function name is getElementsByClassName , not getElementByClassName getElementsByClassName returns an array-like object of elements, not the one of elements. Code should look like this: n = new Date(); y = n.getFullYear(); document.getElementsByClassName("date")[0].innerHTML = y; <span class

Mulitple Elements with the same ID

删除回忆录丶 提交于 2021-01-30 09:09:34
问题 I am trying to use a script on three videos using the same ID (#vid) on the same page. At the moment only one video seems to be able to use the script. var video = document.getElementById('vid') // When the 'ended' event fires video.addEventListener('ended', function(){ // Reset the video to video.currentTime = 0; // And play again video.load(); }); 回答1: Most of these answers are only partially correct. For your markup to be valid, id's need to be be unique . However, sometimes careless

Mulitple Elements with the same ID

一个人想着一个人 提交于 2021-01-30 09:09:03
问题 I am trying to use a script on three videos using the same ID (#vid) on the same page. At the moment only one video seems to be able to use the script. var video = document.getElementById('vid') // When the 'ended' event fires video.addEventListener('ended', function(){ // Reset the video to video.currentTime = 0; // And play again video.load(); }); 回答1: Most of these answers are only partially correct. For your markup to be valid, id's need to be be unique . However, sometimes careless

Vanilla JavaScript adding classes to elements [duplicate]

ⅰ亾dé卋堺 提交于 2021-01-29 11:08:56
问题 This question already has answers here : Adding click event listener to elements with the same class (6 answers) Closed 2 years ago . I would like to use pure JS to add classes to some elements and am familiar with selecting the element with an ID, but when I've tried to use getElementsByClassName, my code breaks. Is it not possible to get elements by class name and add a new class? I know the following code works, but, again, would like to target the element by className. document

Javascript loop through multiple forms, get all input values

吃可爱长大的小学妹 提交于 2021-01-29 09:43:27
问题 I need to build an object from multiple forms input values without using jQuery. <div id="formMain"> <form id="form_id_1" class="formClass"> <div id="fullname"> <p>Full Name</p> <input type="text" class="inputClass" name="name" value="Joe"> <br/> <input type="text" class="inputClass" name="name2" value="Doe"> </div> <div id="Address"> <p>Address</p> <input type="text" class="inputClass" name="address" value="1st Maint Street"> </div> </form> <form id="form_id_2" class="formClass"> <div id=