JQuery :: Binding Multiple Elements Without Using Selector
Dec 21, 2010
I am learning jQuery now and have a big interest in this javascript framework. I am trying to bind an event to more than one element. I won't meet any problem if using selector. But now, I've created a function which I will pass the element. And I need within this function, to bind the code. This is my current code :
function updateDay(dayObj, monthObj, yearObj){
//I want to bind both monthObj and yearObj.
yearObj.change(function(){
//do something
});
monthObj.change(function(){yearObj.change()});
}
$(document).ready(function(){
updateDay( $("#day"), $("#month"), $("#year"));
});
As you can see, I must bind twice, once for yearObj, the other for monthObj. Is there anyway to bind the change() event only once? If there are some logic mistake in using function or such as feel free to point out, since I still haven't read all the documentation yet.
In the examples for live() and delegate(), the selectors match at least one element that already exists. Will either of these commands work on elements for which there is no match at all on page load?
In my case, I want to bind a keyup event to the textareas that jeditable creates. I could probably create custom plug-in (to the plug-in :) to do the job, but I'd like to use live or delegate if they would work.
We know that multiple calls to $(document).ready(); in the same page is possible.
I have a situation where another team implemented a $('#element').live('click', function() { /* Do this */} ); call on #element. I cannot modify this code, but I need to take an additional action on the same element on the same event. So, two actions will occur when #element is clicked.
I tried making $('#element').live('click', function() { /* Do that */} ); but this call does not fire.
It needs to be .live() since #element is being dynamically added.
Is there any way to add more actions to the same event on #element?
I have an element generator using jquery, However, elements generated were not affected by the event (click) even though they match tag and class name.
Let's say $pages is a collection containing all div elements that have an id of page1 or page2. From this, I wish now to select now only the page div (i.e page1 or page2) that contains a descendant with a class of 'A'. Tried lots of things, just can't get the format right.
I've been fiddling around with a bit of javascript in a chrome extension - something to alter the Google buzz webpage.I'm trying to find each individual post basically and have the following:
var entry =$('.X0POSb'); //This main block contains the bulk of Google buzz content console.log(entry); var items = entry.find('.G3.G2');
Is there a more elegant way of combining ORs and ANDs in a selector? I have a long list that I'd like to simplify similar to the example below that is based on multiple classes.
<div "style=width:100px";> <div class="A B"> No </div> <div class="A C "> Yes </div> <div class="A D "> Yes </div> <div class="A E"> Yes </div> <div class="A F"> No </div> <div class="B C "> Yes </div> <div class="B D "> Yes </div> <div class="B E"> Yes </div> <div class="B F"> No </div> <div class="C"> No </div> </div><script> $(".A.C, .A.D, .A.E, .B.C, .B.D, .B.E").css("border", "3px solid red"); </script>
I am using jquery with the cookie plugin and I have multiple image buttons that can hide/show multiple elements. My question is how can I add a cookie to this code to remember whether each separate element is opened or closed?
The code, $(document).ready(function() { // choose text for the show/hide link - can contain HTML (e.g. an image) var showText='<div class="expanddown"></div>'; var hideText='<div class="expandup"></div>'; // initialise the visibility check var is_visible = false; // append show/hide links to the element directly preceding the element with a class of "toggle" $('.toggle').prev().append('<a href="#" class="togglelink">'+hideText+'</a>'); // capture clicks on the toggle links $('a.togglelink').click(function() { // switch visibility is_visible = !is_visible; // change the link depending on whether the element is shown or hidden $(this).html( (!is_visible) ? hideText : showText); // toggle the display - uncomment the next line for a basic "accordion" style //$('.toggle').hide();$('a.toggleLink').html(showText); $(this).parent().next('.toggle').slideToggle('fast'); // return false so any link destination is not followed return false; }); }); HTML, <a class="togglelink" href="#"></a> <div class="toggle"> Content </div>
... but that doesn't work. It seems as though if the first selector returns no elements that the 2nd line is not executed. I might be wrong about the diagnosis, but the symptom is that I can show the log, but can't then hide it, whereas if I start with all the panels visible and remove the first selector, the hideLog event is triggered as expected.What does work is this:
$("#div1, #div2, #div3, #div4).click(function(){ //Get the Current Selected Picked //I Would like to get if the item clicked was the Div1 or Div 2 or Div 3 so I can make changes in the selector chosen }); How do I do that? It's really simple but I tried $(this) but does not work.
In order to allow users to filter content, I've created a small checkbox list. To each checkbox is attached an ID, and too some divs on the page are attached these id's too (in the class attr that is); one div can be attached to multiple id's, and so have multiple classes.
In order to show or hide my div's I wrote : function couponsVisibles(){ $('.checkInteret').each(function(){ var coche = $(this).attr("checked"); if (coche) { var idCoupons = $(this).attr("id"); $(".zoneCoupon , div[class='" +idCoupons+ "']").each(function(){ $(this).fadeIn(); }); }}); } $(".checkInteret").click(function(){ var coche = $(this).attr("checked"); if (!coche) { var idCoupons = $(this).attr("id"); $(".zoneCoupon , div[class='" +idCoupons+ "']").each(function(){ $(this).fadeOut(); }); }couponsVisibles(); }); It looks like my selector "$(".zoneCoupon , div[class='" +idCoupons+"']")" doesn't work, but I can't figure out why. I've started using the [attribute*=value] selector, but this selector doesn't seem to make a difference beetween class='8 10' and class='8 1'...
I'm trying to load some html content into a page via the ajax .load() method (wrapped within the $(document).ready() function).After I execute this, I'd like to bind all new span elements from the loaded content to a context menu plugin like this:
$("#selector").contextMenu({ menu: ''myMenu'' },
[code]....
Unfortunately since the span elements are coming from the ajax request,I don't think I can bind a normal event handler as per the plugin. [URL] how to use event bubbling in this situation.
I'm using the Cycle plugin to display images for some of my past projects onhttp:[URL]..projekteMy problem is that in Chrome i get the above error in console.([cycle] terminating; zero elements found by selector) I have tried manually initializing the Cycle plugin for every project div and that did not help so I went back to my initial code:
i'm trying to selectan input element with multiple selectors:- input element'sclass contains "submitText"- input element'sclassDOES NOT contain "ui-focus"- input element's name is "myName"it should look somehow like this:
$(' input[class*="submitText"][class!*="ui-focus"][name="myName"] ').attr('value', value); i know !*= does notexist. but what elsecan i use? i also tried somethinglike this:
I've selected multiple tables and tried to use each for applying cornering to each table. Inside the callback function, I try to find the first/last table cells to apply cornering for each corner. However, when there are multiple tables it looks at _all_ the tr elements for the first/last rows. See example code:
// Add cornering to tables $('table.corner-me').addClass('ui-corner-all').each(function(){ var $table = $(this); $table.find('tr:first :first-child').addClass('ui-corner-tl');
Can someone explain to me the best way to loop through the geocode and marker (google maps) part of this code for all of the elements with the "address" class using jQuery (or javascript if need be). I tried wrapping the contents of the codeAddress function in $(".address").each(function () {... and replacing the getElementById with a jquery selector, but I still seem to be lost.
But when there are multiple classes, it only deletes the first element in its query. As a successful test, I appended some CSS to ensure the query selected all classes and added a border to each element. So why does remove() not work? Is it a bug or is my query missing something?
I just started using jquery a few days ago and am already addicted.Now I am trying to figure out one thing which I can't seem to get answered by Google:I am building a dynamic website where at one place there will be an unclear number of div-elements. So there can be none, just one or several.The plan is to show them by a click on a button via slideDown(). So I wrapped them with another div.wrapper via wrapAll(). This works nicely for any number.Now the thing is: if my .wrapper has no specified height, the slideDown jumps - which doesn't look very nice.So I gave it a height:$('.wrapper').css({'height': $('div').outerHeight()});Presto, the animation works smoothly. But this works only if there's just one div.
I am putting together a validation function and am having trouble with targeting multiple elements.[code]an onclick event fires my validation function. If any of the fields are empty, the appropriate error message should display.This sort of works. If all of the fields are empty, only the error message of the first element is displayed, not all 3.