Tag: dom

Performing an action on Parent/Child elements independently

I have HTML similar to the following in my page <div id=”someDiv”> <img src=”foo.gif” class=”someImg” /> </div> The wrapper div is set up such that when it is clicked, it’s background-color changes using the following jQuery code. $(“div”).click(function(event){ $(this).css(“background-color”, “blue”); }); I also have some jQuery associated with my img that will do some other […]

Js-jQuery – check if element is focused or get last focused element class

is it possible to check if an element is focused? i tryed: <a href=”#”></a> $(‘a’).is(‘:focused’); but it won’t work. Also i would like to understand if is possible to get the last focused document element attr class: var last_focused_class = document.lastFocusedElement.class; for example

How do I apply events to dynamicly loaded DOM nodes with JQuery?

I’m using AHAH to load a piece of HTML after the document is ready. There’s a button in that chunk of HTML I would like to apply a .click event to. I’m having trouble applying that event after the HTML is loaded. The call back is very generic and is used by other parts of […]

jQuery change() and bind(“change”) do not work

This question has already been asked but I am stuck with the most basic level of it. I haven’t added anything to my html except a select tag and trying to catch the change event by jquery. Here is my code: <head> <script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js”></script> <script> $(‘#target’).bind(“change”, function{ alert(‘Changed’); }); </script> </head> <body> <form> <select id=”target”> […]

check if css class exist and if yes how many times

I’d like to know if a style class exists and if yes, how many elements have it. To know if a style class exists, I use: if ($(“*”).hasClass(‘ui-state-active’)) { alert(“class exist : “+nb_checked); } But to know how many elements have the class, I can’t figure out.

Help me understand objects in DOM

I have a question about DOM objects. What I have learned is that everything in a document are objects? <form name=”myform” action=”script.js”> <input type=”text”>type something</input> </form> If we look at the example, is form an object with properties name and action? But are name and action themselves also objects? i don’t quite understand the difference […]

not able to set ID's to dynamically created labels in JQUERY

i created a label dynamically in jquery. I need to assign certain styles to that label. Here i tried to assign a unique ID to that label and apply styles. But it’s not working. my code is; Javascript var lbl = $(“<label>”).text(‘Welcome’); lbl.id = “test”; $(“#sec”).append(lbl); CSS #test { color:red; font-size:20px; } how to assign […]

Trying to cast element ID to class selector in jQuery

Here is my jsfiddle: http://jsfiddle.net/fk434/ The code: $(‘.theHider’).click(function () { $(“‘.” + $(this).id + “‘”).hide(); }); The HTML: <div class=”cola”>This will be cola</div> <div class=”cola”>This will be cola</div> <div class=”birch”>This will be birch</div> <div class=”cola”>This will be cola</div> <div class=”cola”>This will be cola</div> <div class=”orange”>This will be orange</div> <div class=”birch”>This will be birch</div> <div id=”cola” […]

Appending elements to DOM in a loop structure

Once the page has been loaded, I would like to append an additional element for each existing elements on the page. I tried something like this: var divs=document.getElementsByTagName(‘div’); for(i=0;i<divs.length;i++){ newDiv=document.createElement(‘div’); divs[i].appendChild(newDiv); } Just a warning this will actually freezes the browser because the divs variable is dynamic and divs.length just gets larger and larger each […]

How to exclude the last element in an unordered list using jQuery

I have an unordered list (ul) and a few list’s elements (li).I’d like to bind all the li‘s except the last one. Example: $(‘ul li’).on(‘click’, someFunction); $(‘ul li:last-child’).off(‘click’); Using jQuery, is there a way to do it in one line of code? Any help will be appreciated!