views:

44

answers:

3

I'm having some trouble figuring this out.. I want to make it so I can place a button in a number of different DIVs but have all the info for the button be the same (class, type, value, etc), this is because its an "edit" button for the DIV so its just something that is automatically included with any new DIV that is created and my server-side app will generate these buttons automatically.. so the issue is how to get the ID of the parent DIV, and I am having some trouble with this as it seems to always default to DIV "upd1" even when I click the button contained within "upd2", I've been searching on this but everything I have found so far hasn't led me out of this issue.. thanks for any advice

<div id="upd1">
<input id= "button" class="button" type="submit" value="Click to edit this Panel" /> 
</div>

<div id="upd2">
<input id= "button"  class="button" type="submit" value="Click to edit this Panel" /> 
</div>

then I have:

$("#button").click(function() {


              dividediting = $("#button").closest("div[id^='upd']").attr("id");
              alert(dividediting);
            }); 
+3  A: 

Try this:

$(".button").click(function() { 
   var dividediting = $(this).parent().attr('id');
   alert(dividediting); 
});  

You cannot have two items with the same id - you have to be using a class not the same id on each button.

Graphain
oh, can't believe I was missing that, I was using "this" earlier for something else, that makes complete sense now..will try that
Rick
This will blow up, jQuery objects don't have an `.id` property on them...
Nick Craver
@Nick interesting, I amended (I usually use attr("id") but get blasted for not using standard JS functions).
Graphain
@Graphain - You would need `.parent()[0].id` or `.parent().get(0).id` for that, which would be valid as well (it *must* have *some* parent).
Nick Craver
@Rick, you can't use the same id on two objects
Graphain
@Nick ah thanks, I just turned it back to attr("id") like I had originally.
Graphain
awesome, the $(this) fixed it.. (I also changed it to use the class instead of id) thanks.. also, it seems you can have 2 ids but it just will take the first (meaning the HTML page still renders but obviously its of no use to have 2)
Rick
@Rick Glad to hear. Yeah it compensates and renders, but it's invalid and JS won't work with it. It's like if I leave tags open, the browser still has a guess at making them work but you're dealing with "guess" behaviour, not "reliable" behaviour.
Graphain
A: 

You need a .class selector for your buttons instead, like this:

$(".button").click(function() {
    dividediting = $(this).closest("div[id^='upd']").attr("id");
    alert(dividediting);
}); 

#button is an #ID selector that searches for an id="button" instead of a class="button" like you want. Also you want $(this) inside the handler, so you're getting the closest <div> of the clicked element, not the first matched .button element.

Nick Craver
The `.class` won't fix the fact the OP is using `#button` in the internal selector.
Graphain
You need .class in the second line as well, `$(".button")`
phoffer
@Graphain - Oops missed the internal, fixed, @phoffer actually you want `$(this)` instead in there.
Nick Craver
@Nick Craver Ya you're right. I got stuck on that little `#` and forgot that.
phoffer
A: 

first of all, why are you using $("#button") i didn't see any id="button" in your DOM, secondly, you can use this

var dividediting = $(".button").parent().attr("id");
alert(dividediting);
rob waminal
my mistake, I had id= "button" in my actual script, I just over-edited it when I put it into the post here.. I changed it
Rick
@rob no the OP cannot, that will give the first button's parent, not the clicked one's.
Graphain
ahh yes, because there are 2 `.button` classes in your DOM so the first `.button` will be taken, but if you are calling it inside an event you can use `this` instead of `.button` selector just like Graphain's example.
rob waminal