Loading content into parent element using jquery
I have a div element with a specific id
<div id='someid'>
<input type='submit' class='edit' value='Edit'>
</div>
When the button is clicked, I want to select the parent element (in this case the div element) and load the content into it. I tried:
$(document).ready(function(){
$(".edit").click(function(){
var id = $(this).parent().get(0).id;
$(id).load("contentpage");
});
});
But that doesn't work, presumably because I'm not picking the right parent? If I print out the id variable it has the correct value ("someid"), but I think $ (id) is where I am going wrong?
+2
astrofrog
source
to share
3 answers
The reason it doesn't work is because your selector is "someid" instead of "#someid", but there is an easier way:
$(document).ready(function(){
$(".edit").click(function(){
$(this).parent().load("contentpage");
});
});
+3
Greg
source
to share
Just connect the chain. You are choosing the wrong identifier.
$(this).parent().load("contentpage")
+2
Jeff Meatball Yang
source
to share
You don't need to use another query, it parent
already returns the parent:
$(document).ready(function(){
$(".edit").click(function(){
$(this).parent().load("contentpage");
});
});
+1
Gumbo
source
to share