tags:

views:

16

answers:

3

Hey guys, quick question,

I have an id that is applied multiple times on one of my pages (I used multiple duplicate ids because I also have a class applied that determines if the icon is a certain color rather than just duplicate the id css for each button color). I am using a jquery form handler that initiates on form submission and I would like to add an attribute to the submit button but since there is another button on the same page with the same id, I am having difficulty getting this to work. Is there a way to select an input by id of the parent form?

For example (I know it is wrong but it is what I want to achieve that matters in this example)

$("form#invite").submit(function() {


$(this).("#buttonid").attr({ disabled:true, value:"Sending..." });
+1  A: 

This should do the trick.

$("form#invite").find("#buttonid").attr({ disabled:true, value:"Sending..." });

Sinan Y.
I appreciate your time Sinan, but this actually did not work. I ended up just modifying the css of my document as suggested.
Scarface
+1  A: 

You should really try to find a way to keep the id's unique. Like using a classname or not naming them at all. In any case, here is the answer to you question:

$("form#invite").submit(function() {
    $(this).find("#buttonid").attr({ disabled:true, value:"Sending..." });
}
Fabian
thanks fabian...
Scarface
+1  A: 

The id attribute must be unique in a HTML document. Multiple elements with the same ID will cause you no end of grief, more-so in some browsers than in others.

To answer your question, you can use find() or selector context:

// Selector context, supplying the current element as the second argument:
$("#buttonid", this).attr({ disabled:true, value:"Sending..." });

// find()
$(this).find("#buttonid").attr({ disabled:true, value:"Sending..." });
Andy E
This *shouldn't* work, because it'll just optimize it to `document.getElementById('buttonid')`...jQuery assumes IDs are unique.
Nick Craver
@Nick: I didn't notice the multiple id thing until after - I ashamedly skim-read the question (it's late). I think the result varies per browser - I thought `querySelectorAll()` was given priority if available?
Andy E
I just modified the css, you are correct in suggesting to keep ids unique, but this suggestion is still useful for future reference in similar situations. Thanks Andy.
Scarface
@Andy - Sometimes...but in this case jQuery will ignore everything and shoot for `getElementById()`, it does this if it detects a `#id` pattern, check it out here: http://github.com/jquery/jquery/blob/master/src/core.js#L120
Nick Craver
@Nick: [it does work](http://www.jsfiddle.net/tMdtK/), tested in IE, Fx and Chrome. Looks like jQuery/Sizzle is smart enough to account for this with selector context/find. Of course, I don't advocate dupe ids in the slightest :-)
Andy E
@Andy - You're right, Sizzle has a work around for this internally, really slowing down the ID selector along the way as well...I guess John guessed if you're using a context with an ID selector, something was up and it attempts to handle it.
Nick Craver