I am building a page with various anchors and form-elements that can be focused and I am wondering how I can get the element that is being focused on currently if something is being focused. I think one way to do it in jQuery is with .is(:focus). I'm looking for either a jQuery or vanilla js solution— just something that works.  Here is what I am trying to do in psuedo-code:  if (something is focused){     get the tag name of that which is currently focused and store that in a variable as a string. } else {     alert("nothing is being focused on"); }   Also, if you could answer this question, please do: Isn't something always focused? Meaning it is the document / body that is focused when no specific elements are or is nothing focused until done so by the code or user?
34827
I would like to find out, in JavaScript, which element currently has focus. I've been looking through the DOM and haven't found what I need, yet. Is there a way to do this, and how?  The reason I was looking for this:  I'm trying to make keys like the arrows and enter navigate through a table of input elements. Tab works now, but enter, and arrows do not by default it seems. I've got the key handling part set up but now I need to figure out how to move the focus over in the event handling functions.
10911
With jQuery, how do I find out which key was pressed when I bind to the keypress event?  $('#searchbox input').bind('keypress', function(e) {});   I want to trigger a submit when ENTER is pressed.  [Update]  Even though I found the (or better: one) answer myself, there seems to be some room for variation ;)  Is there a difference between keyCode and which - especially if I'm just looking for ENTER, which will never be a unicode key?  Do some browsers provide one property and others provide the other one?
38027
I need a way to determine the type of an HTML element in JavaScript. It has the ID, but the element itself could be a &lt;div&gt;, a &lt;form&gt; field, a &lt;fieldset&gt;, etc. How can I achieve this?
32885
I have some html like this:  &lt;div id="1"&gt;     &lt;p&gt;         Volume = &lt;input type="text" /&gt;         &lt;button rel="3.93e-6" class="1" type="button"&gt;Check answer&lt;/button&gt;     &lt;/p&gt;     &lt;div&gt;&lt;/div&gt; &lt;/div&gt;   and some JS like this:  $("button").click(function () {     var buttonNo = $(this).attr('class');     var correct = Number($(this).attr('rel'));      validate (Number($("#"+buttonNo+" input").val()),correct);     $("#"+buttonNo+" div").html(feedback); });   What I'd really like is if I didn't have to have the class="1" on the button (I know numeric classes aren't valid, but this is a WIP!), so I could determine buttonNo based on the id of the parent div. In real life there are multiple sections looking like this.   How do I find the id of the div parenting the button. What would be a more semantic way to store the answer in the button code. I want to make this as foolproof as possible for a non programmer to copy and paste without breaking things!
5173
Is there an efficient way to tell if a DOM element (in an HTML document) is currently visible (appears in the viewport)?  (The question refers to Firefox.)
65967
I am trying to read a custom (non-standard) CSS property, set in a stylesheet (not the inline style attribute) and get its value. Take this CSS for example:  #someElement {   foo: 'bar'; }   I have managed to get its value with the currentStyle property in IE7:  var element = document.getElementById('someElement'); var val = element.currentStyle.foo;   But currentStyle is MS-specific. So I tried getComputedStyle() in Firefox 3 and Safari 3:  var val = getComputedStyle(element,null).foo;   ...and it returns undefined. Does anyone know a cross-browser way of retreiving a custom CSS property value?  (As you might have noticed, this isn't valid CSS. But it should work as long as the value follows the correct syntax. A better property name would be "-myNameSpace-foo" or something.)
85136
All right, say I have this:  &lt;select id='list'&gt;     &lt;option value='1'&gt;Option A&lt;/option&gt;     &lt;option value='2'&gt;Option B&lt;/option&gt;     &lt;option value='3'&gt;Option C&lt;/option&gt; &lt;/select&gt;   What would the selector look like if I wanted to get "Option B" when I have the value '2'?  Please note that this is not asking how to get the selected text value, but just any one of them, whether selected or not, depending on the value attribute. I tried:  $("#list[value='2']").text();   But it is not working.
46429
I've never gotten is_active_sidebar() to work, no matter if there's widgets in the sidebar or not I always get false returned. Currently I'm creating a sidebar for each top level page:    I create my own widget(s):    And I'm testing whether the sidebar is active like so:  if(is_active_sidebar(get_the_title()))     echo 'active'; else     echo 'not active';   Even when I manually put the title in: is_active_sidebar('Test'); it always returns false. Am I using the conditional wrong? Do I need to add some sort of setting? Why would is_active_sidebar() fail?
100723
I am building an application where I want to be able to click a rectangle represented by a DIV, and then use the keyboard to move that DIV by listing for keyboard events.  Rather than using an event listener for those keyboard events at the document level, can I listen for keyboard events at the DIV level, perhaps by giving it keyboard focus?  Here's a simplified sample to illustrate the problem:  &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt;  &lt;div id="outer" style="background-color:#eeeeee;padding:10px"&gt; outer     &lt;div id="inner" style="background-color:#bbbbbb;width:50%;margin:10px;padding:10px;"&gt;    want to be able to focus this element and pick up keypresses    &lt;/div&gt; &lt;/div&gt;  &lt;script language="Javascript"&gt;  function onClick() {     document.getElementById('inner').innerHTML="clicked";     document.getElementById('inner').focus();  }  //this handler is never called function onKeypressDiv() {     document.getElementById('inner').innerHTML="keypress on div"; }  function onKeypressDoc() {     document.getElementById('inner').innerHTML="keypress on doc"; }  //install event handlers document.getElementById('inner').addEventListener("click", onClick, false); document.getElementById('inner').addEventListener("keypress", onKeypressDiv, false); document.addEventListener("keypress", onKeypressDoc, false);  &lt;/script&gt;  &lt;/body&gt; &lt;/html&gt;   On clicking the inner DIV I try to give it focus, but subsequent keyboard events are always picked up at the document level, not my DIV level event listener.  Do I simply need to implement an application-specific notion of keyboard focus?  I should add I only need this to work in Firefox.
11521
Can someone show me how to get the top &amp; left position of a div or span element when one is not specified? ie: &lt;span id='11a' style='top:55px;' onmouseover=&quot;GetPos(this);&quot;&gt;stuff&lt;/span&gt; &lt;span id='12a' onmouseover=&quot;GetPos(this);&quot;&gt;stuff&lt;/span&gt;  In the above, if I do: document.getElementById('11a').style.top  The value of 55px is returned. However, if I try that for span '12a', then nothing gets returned. I have a bunch of div/spans on a page that I cannot specify the top/left properties for, but I need to display a div directly under that element.
93461
What is the preferred/easiest way to find the control that is currently receiving user (keyboard) input in WinForms?  So far I have come up with the following:  public static Control FindFocusedControl(Control control) {     var container = control as ContainerControl;     return (null != container         ? FindFocusedControl(container.ActiveControl)         : control); }   From a form, this can be called simply as (in .NET 3.5+ this could even be defined as an extension method on the form) -  var focused = FindFocusedControl(this);   Is this appropriate?  Is there a built-in method that I should be using instead?  Note that a single call to ActiveControl is not enough when hierarchies are used. Imagine:  Form     TableLayoutPanel         FlowLayoutPanel             TextBox (focused)   (formInstance).ActiveControl will return reference to TableLayoutPanel, not the TextBox (because ActiveControl seems to only be returning immediate active child in the control tree, while I'm looking for the leaf control).
37425
I have a document with headings and unordered lists. How can I use JQuery to select a given heading (by its unique class name) AND all content between that heading and the next heading? Update: Your suggestions are great, but aren't what I'm looking for. In the below code, for example, I would like to access only the &quot;h1&quot; with id of &quot;heading2&quot; and everything up to, but not including the &quot;h1&quot; with id of &quot;heading3&quot;. The jQuery examples provided above will access everyting after the first &quot;h&quot; tag that is not an &quot;h&quot; tag. ... or, correct me if I'm wrong :)     &lt;h1 id=&quot;heading1&quot;&gt;...&lt;/h1&gt;         &lt;ul&gt;...&lt;/ul&gt;         &lt;p&gt;...&lt;/p&gt;         &lt;ul&gt;...&lt;/ul&gt;         &lt;p&gt;...&lt;/p&gt;     &lt;h1 id=&quot;heading2&quot; &gt;...&lt;/h1&gt;         &lt;ul&gt;...&lt;/ul&gt;         &lt;p&gt;...&lt;/p&gt;         &lt;ul&gt;...&lt;/ul&gt;         &lt;p&gt;...&lt;/p&gt;     &lt;h1 id=&quot;heading3&quot; &gt;...&lt;/h1&gt;         &lt;ul&gt;...&lt;/ul&gt;         &lt;p&gt;...&lt;/p&gt;         &lt;ul&gt;...&lt;/ul&gt;         &lt;p&gt;...&lt;/p&gt;
42629
For instance in the snippet below - how do I access the h1 element knowing the ID of parent element (header-inner div)?  &lt;div id='header-inner'&gt;     &lt;div class='titlewrapper'&gt;        &lt;h1 class='title'&gt;        Some text I want to change       &lt;/h1&gt;     &lt;/div&gt;  &lt;/div&gt;   Thanks!
80897
I'm trying to find a way with javascript to highlight the text the user selects when they click some odd highlight button (as in &lt;span style="background-color:yellow"&gt;highlighted text&lt;/span&gt;). It only has to work with either WebKit or Firefox, but it seems to be well nigh impossible because it has to work in the following cases:  &lt;p&gt;this is text&lt;/p&gt; &lt;p&gt;I eat food&lt;/p&gt;   When the user selects from "is text" through "I eat" in the browser (can't just put a span there).  and this case:  &lt;span&gt;&lt;span&gt;this is text&lt;/span&gt;middle text&lt;span&gt;this is text&lt;/span&gt;&lt;/span&gt;   When the user selects from "is text" to "this is" in the browser (even though you can wrap your highlight spans around each element in the selection, I'd like to see you try to get that middle text highlighted).  This problem doesn't seem to be solved anywhere, frankly I doubt it's possible.  It would be possible if you could get the Range that you get from the selection as a string complete with html which could be parsed and then replaced, but as far as I can tell you can't get the raw html of a Range.. pity.
7172
What would be the best way to look in a string[] to see if it contains a element. This was my first shot at it. But perhaps there is something that I am overlooking. The array size will be no larger than 200 elements.  bool isStringInArray(string[] strArray, string key) {     for (int i = 0; i &lt;= strArray.Length - 1; i++)         if (strArray[i] == key)             return true;     return false; }
3010
I have created a panel page (i.e using panels). I can add html content to it from the panel itself using add content->New custom content. I can also add css classes (add body css classes).  Now I would like to add a jquery button to that panel page. I could add a javascript to my theme but then it would be called on every page load not only this specific panel-page. So I would like to add my javascript through my module.   But how do I programatically check that the current page beeing viewed is that specific page panel? Is there a hook that lets me check that?  Thanks,  baba
27745
Is there a way to programmatically determine which document class is in effect?  I'd like to be able to write conditional macros that behave differently depending on which document class is currently being used.