dom-events

Validation of textarea

对着背影说爱祢 提交于 2019-12-04 04:21:20
How to do I validate a textarea in a form? i.e, it should not be empty or have any new lines, and if so raise an alert. Code: <script> function val() { //ifnewline found or blank raise an alert } </script> <form> <textarea name = "pt_text" rows = "8" cols = "8" class = "input" WRAP ></textarea> <input type=""button" onclick="val();" </form> The easiest way I could think of: function validate() { var val = document.getElementById('textarea').value; if (/^\s*$/g.test(val) || val.indexOf('\n') != -1) { alert('Wrong content!'); } } Sarfraz Try this: <textarea id="txt" name = "pt_text" rows = "8"

Files input change event fires only once

岁酱吖の 提交于 2019-12-04 03:19:14
I've made a small script using some of the HTML5 files features, which allows you to select one or more files, and each time it will write the name of the file(s). Everything works as it should, only the event to detect the value change of the files input fire only once, so how can I make it fire every change and not only on the first change? By the way, here is what I made: http://tamir.netspot.co.il/html5/files/ It appears that the change event listener is being removed because you're using innerHTML to update the same element ( wrapper ) that the input itself is inside. So the contents of

Does Javascript event queue have priority?

别等时光非礼了梦想. 提交于 2019-12-04 03:17:33
These days, I have read some documents about setTimeout, setInterval. I have learned that the Javascript is a single thread which will only execute one piece of code per time. At the same time, if there is a event happens, it will be pushed into the event queue and block until appropriate time. I want to to know, when many events are blocked waiting to execute at the same time. Is these events have different priorities, so the high priority event will execute before the low ones. Or just a FIFO queue. setTimeout(fn1, 10); $(document).click(fn2); //will be called at 6ms; $.ajax({ajaxSuccess(fn3

Pass parameter to callback function

耗尽温柔 提交于 2019-12-04 02:20:33
My code // do ajax request and get JSON response for (var i = 0; i < data.results.length; i++) { result = data.results[i]; // do stuff and create google maps marker marker = new google.maps.Marker({ position: new google.maps.LatLng(result.lat, result.lng), map: map, id: result.id }); google.maps.event.addListener(marker, 'click', function() { createWindow(marker.id); //<==== this doesn't work because marker always points to the last results when this function is called }); } How to solve this? Try this: with ({ mark: marker }) { google.maps.event.addListener(mark, 'click', function() {

Passing values in for loop to event listeners- Javascript [duplicate]

喜夏-厌秋 提交于 2019-12-04 02:13:33
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Looping through Markers with Google Maps API v3 Problem I have a loop in which I extract some data from an array element, then add an event listener for each iteration. But I need to pass these values to each associated listener but I end up having only the last array item values in the listener. I have tried to understand from "this" but the solution seems not suitable in my case and I am confused. This is the

angularjs: broadcast from directive to controller

一曲冷凌霜 提交于 2019-12-03 23:56:56
问题 Im trying to send a message from within a directive to its parent controller (without success) Here is my HTML <div ng-controller="Ctrl"> <my-elem/> </div> Here is the code in the controller which listens for the event $scope.on('go', function(){ .... }) ; And finally the directive looks like angular.module('App').directive('myElem', function () { return { restrict: 'E', templateUrl: '/views/my-elem.html', link: function ($scope, $element, $attrs) { $element.on('click', function() { console

Why does a `click` event get triggered on my <button> when I press Enter on it?

落花浮王杯 提交于 2019-12-03 23:47:39
I'm only adding a click event handler on my <button> . document.getElementsByTagName("button")[0].addEventListener("click", event => { event.preventDefault(); console.log("Click:", event); }); <button>Press <kbd>Enter</kbd> on me</button> ( Demo link ) Nevertheless, when I tab to the button in Firefox, then press Enter , I see the click event being fired. However, I cannot see this behaviour documented anywhere. Is this standard behaviour, and can I count on it working in all browsers? This is largely because lots of authors have historically written code using click events while forgetting to

How do you remove an event listener that uses “this” in TypeScript?

本秂侑毒 提交于 2019-12-03 23:41:11
In JavaScript, for an event handler that needs access to private members and functions, I can rely on the function scope of those to be accessible within my event handler function, and do something like this: theElement.addEventListener("click", onClick); and later: theElement.removeEventListener("click", onClick); In TypeScript, I need to use an anonymous function to have this be the containing object, like so: theElement.addEventListener("click", (event) => this.onClick(event)); In this case, I can't remove the anonymous function from the listening to the event. How do I have an event

Uncaught (in promise) TypeError: Request failed pwa error

末鹿安然 提交于 2019-12-03 21:58:56
I am trying to build a Progressive Web App (PWA). The service worker shows registered, PWA audit shows everything good, but the console shows this error: TypeError: Request failed pwa error I am unable to cache the files. I tried all possible answers on Stack Overflow and it's the same //self.addEventListener('fetch', function(event) {}); var dataCacheName = 'myappData-v3n'; var cacheName = 'myapp-3n'; var filesToCache = [ 'images/logo.png', 'js/jquery.min.js', 'js/popper.min.js', 'js/bootstrap.min.js', 'js/main.js', 'css/bootstrap.min.css', 'css/fontawesome-all.min.css', 'css/style.css', 'css

How can I check if Shift+Enter is pressed on a textarea

女生的网名这么多〃 提交于 2019-12-03 21:27:44
I want to submit the form if Enter is pressed and stop the event if Shift + Enter is pressed. The callback for this has Ext.EventObject parameter which does not provide any way to check if shiftkey is pressed. it has two methods .hasModifier and .isSpecialKey . Both returns boolean . There is no way to find if shiftkey is pressed. how do I trace it? This is my textarea component: { region : 'center', margins : '5 0 0 0', xtype : 'textarea', name : 'chatmessage', enableKeyEvents: true, listeners: { keydown: function(textfield, evt, eOpts){ console.log(evt.getKey()); } } } I tried evt.shiftKey .