I've just created my first element as a test and I'm running into an issue with an on-click event. My element is simply a button with a click event to increment a counter which is displayed as part of the button text. I've also added a content tag so you can add additional text to the button.
When I go to click the button in Chrome, if I click on the content text, the click event isn't fired. I have to actually click on the button element / the count in the button to fire the event. All seems to be working properly though in Firefox, Safari and Opera. I can click anywhere on the button in those browsers and the click event will fire. Am I doing something wrong? Is this just a bug with Chrome? Here is my code:
index.html
<!DOCTYPE html>
<html>
<head>
<title>Test Element</title>
<script src="bower_components/platform/platform.js"></script>
<link rel="import" href="elements/my-counter.html">
</head>
<body unresolved touch-action="auto">
<my-counter count=5>Times a Day</my-counter>
</body>
</html>
elements/my-counter.html
<link rel="import" href="../bower_components/polymer/polymer.html">
<polymer-element name="my-counter" attributes="count">
<template>
<button on-click="{{increment}}">{{ count }} <content></content></button>
</template>
<script>
Polymer('my-counter', {
count: 0,
increment: function(event, detail, sender) {
console.log(event, detail, sender);
++this.count;
}
});
</script>
</polymer-element>
This code is also on GitHub if you would like to download it and test it yourself: https://github.com/andersryanc/playing-with-polymer/tree/master/my-counter
If you wrap your extra text in a span it works.
<my-counter><span>Times a Day</span></my-counter>
See this JSbin.
I guess this is related to the fact that textNodesdon't fire most events (see here).
If you mix LightDOM and ShadowDOM with events, make sure you read this article and this SO thread.
The reason that your code behaves differently in Chrome vs other browsers is that Chrome is currently the only browser shipping a native shadow dom implementation. Firefox is set to ship theirs in a couple weeks. More up to date info here: caniuseit - shadowdom
Related
I know IE9 is kind of old now, but it is the lowest version of IE that I still must support in a Web application I'm building.
Anyway, while doing some DOM manipulation and testing in different browsers, I noticed that IE9 was doing something very strange. I had a <script> block in a DIV element, and when I deep-cloned that DIV element using Node.cloneNode(true), and attached the clone to the document somewhere using document.body.appendChild or document.body.insertBefore, the cloned <script> block would get executed again! No other browser exhibits this behavior.
If I'm not mistaken, <script> blocks aren't supposed to be executed when appended to the document after the document has initially loaded, am I right? If I'm correct, is this a bug in IE9?
Here is a simple HTML document where you can see this behavior in action. Create an HTML document with this code and load it up in Internet Explorer using IE9 emulation. You should see an alert popup that says "hey". Next, click the "Click Me" button, and you will see the same popup get executed again!
<!DOCTYPE html>
<html>
<head>
<title>IE9 Script Tag Bug Test</title>
<script>
function ButtonClick(){
var Elem = document.getElementById('mydiv');
var ElemClone = Elem.cloneNode(true);
document.body.insertBefore(ElemClone,Elem);
}
</script>
</head>
<body>
<div id="mydiv">
This is a DIV.
<script>
alert("hey");
</script>
</div>
<button onclick="ButtonClick();">Click Me</button>
</body>
</html>
This may be simply impossible, judging from the many comments on this topic in StackOverflow from several years ago. However, in case I am missing something, here's the situation:
I have an html canvas element. When the user taps it, I make an input element visible. Ideally, the input gets the focus and on a mobile device, the native keyboard pops up.
And that's what happens on an Android device. However, when an iPhone user on Safari taps the canvas, the input appears but it doesn't get the focus and the native keyboard does not appear. The user has to tap the input element for the native keyboard to show up.
Does anyone have a way to make the keyboard show up on an iPhone without that last extra tap?
Here is a test case:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<div id="box" style="width:100px;height:100px;position:relative;background-color:blue"></div>
<div id="holder">
<form>
<input id="in">
</form>
</div>
</body>
<script>
$(function() {
$("#holder").hide();
$("#box").mousedown(function() {
$("#holder").show({complete:function() {$("#in").focus();}});
});
});
</script>
</html>
Found a combination of strategies that works. Not sure why, but if I handle the tap in 'touchstart' instead of 'mousedown,' and add an extra call to 'focus,' the IOS device awards me focus and native keyboard:
$("#box").on("touchstart", function(e) {
$("#holder").show({complete:function() {$("#in").focus();}});
event.stopPropagation();
event.preventDefault();
$("#in").focus();
});
I am trying to build a simple webpage that replaces the contents of the <div id="body"> with something new based on the user clicking on a "link"
The following code does exactly what I expect in Chrome and Firefox, but does nothing (except turn the link to the visited color) in IE 10 or 11:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#activities").click(function(){
$("#body").load("test02.html");
});
});
</script>
</head>
<body>
<div id="header">
Activities
<!-- this stays the same -->
</div>
<div id="body">
<p>this is the content that arrives without needing prompting</p>
<!-- All content will be loaded here dynamically -->
</div>
<div id="footer">
<!-- this stays the same -->
</div>
</body>
</html>
This is the content of "test02.html":
<p>---</p>
<p>Hello world!</p>
<p>---</p>
I've checked the javascript security settings in IE and everything is set to "enable." I've also tried adding type="text/javascript" to the script tags.
Some amount of Googling has turned up the possible need to reinstall IE, which I have tried.
Anyone have an idea about how to get this working in IE?
The problem is that IE breaks itself in "compatibility" mode. The way in which it breaks itself in this case is failing to correctly look up your div id="body" element. I think that was observation error on my part, I think the real problem is addEventListener (because jQuery 2.x doesn't fall back to attachEvent anymore, since it doesn't support IE8 and earlier [or the "compatibility" modes that act like them]):
I can replicate the problem. The problem goes away if I tell IE not to break itself (e.g., not to use compatibility mode) by adding this to the top of the head element:
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
IE's default for intranet sites is to display them in "compatibility" mode.
At one point I wasn't at all sure that when in "compatibility" mode it didn't get confused about that element with the id "body". IE has a history of getting confused by things like that. So you might also consider the-body or similar, but I tested and didn't seem to need it.
Side note: You probably also want to add a return false or e.preventDefault() to your click handler, so it doesn't follow the # link (which will scroll back to the top of the page and add # to the address bar).
add meta tag below to your page
<meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />
Do you mean <body> tag or <div id="body"> and do you ever try preventing default behavior of the link by using this below code :
$(document).ready(function(){
$("#activities").click(function( e ){
e.preventDefault(); //<---- add here
$("#body").load("test02.html");
});
});
I use inline Ckeditor to edit content. I want to bind a keypress event to the div i edit. I mean, i need an event that will fire when i change the content of div.
Here is an example of how i do that
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.2/ckeditor.js"></script>
</head>
<body>
<div id="ckediv" contenteditable="true">Editing with CKEDITOR</div>
<br>
<script type='text/javascript'>
$( "#ckediv" ).keypress(function() {
alert('cke key pressed');
});
</script>
</body>
</html>
The problem is that keypress is not fired in ie and chrome when i press enter ordelete keys. If i make a div with contenteditable="true" but without Ckeditor then the event works well.
Here is a jsfiddle with code that shows how it works now http://jsfiddle.net/uAc7c/4/ .I don't know why, but for some reason this jsfiddle(keypress event) doesn't work in ie. When i tested locally with above source, it worked.
And here is a jsfiddle without Ckeditor that shows how it should work http://jsfiddle.net/mPM4J/4/
JQuery Documentation says:
Note: as the keypress event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms.
So i guess IE and Chrome are two of the unsupported browsers.
Therefore try using the keyup event instead like this:
$( "#ckediv" ).keyup(function() {
alert('cke key pressed');
});
For more info, see here:
KeyUp Documentation in the JQuery API
The following code is throwing two alerts as expected in IE but not in Firefox. Please help.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function myFunction(){
alert('myfunc');
document.getElementById('mylabel').click();
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<p id='mylabel' onclick="alert('you reached');"></p>
<input type='button' value="Click me" onclick='myFunction();'/>
</BODY>
</HTML>
Firefox only has a click() function for form elements such as buttons. However, you can call the onClick function directly; you can change the line to
document.getElementById('mylabel').onclick();
This works in firefox or IE (but note that it requires that the function actually exists, which you know it does in this example).
Also note that you aren't actually simulating a click on that element (so, for example, if there were other things that such a click would do, such as also act as a click on the container, they won't happen). You're just getting the function that would run on a click, and running it directly. So it's not a solution for all situations where you need to simulate a click.
There's no click method on elements. Are you using any library?
Usually you have to do something like element.fireEvent('click') (prototype, mootools)
or element.click() (jquery)
UPDATE- Similar question: How do I programmatically click on an element in JavaScript?
Looks like an ugly and brittle solution, if I were you I'd just include jQuery and let that handle all the browser quirks.
Because the <p> tag does not have the method click.