Reputation:
I wanted to put an id in my element's parent element. Below is my code:
<div>
<div id="child"></div>
<div>
Im aware that jquery has a way to select a parent element , but I dont know how what method shall I use to put an id to it. Below is my jquery code:
div_resizable = $( "#child" ).parent();
div_resizable.id = "div_resizable";
Above code doesnt work with me. It doesnt throw an error, but no changes had taken effect. Any solution to my problem?
Upvotes: 1
Views: 886
Reputation:
For achieve what you want, you can use the jquery attr:
$("#child" ).parent().attr('id', 'newID');
Or you can use the prop:
$("#child" ).parent().prop('id', 'newID');
And you can check the difference between the two here: difference between prop() and attr()
Upvotes: 2
Reputation: 173642
To set a property on the first element inside a jQuery result object:
div_resizable = $( "#child" ).parent()[0];
// ^^^
div_resizable.id = "div_resizable";
This picks the first Element
from the result so that you can directly access the property.
Or:
$('#child').parent().prop('id', 'div_resizable');
Use the .prop()
helper method to accomplish the same thing.
Upvotes: 0
Reputation: 94349
Of course div_resizable.id = "div_resizable"
doesn't work. div_resizeable
is an jQuery array and you are trying to assign id
to it.
Try .attr
instead:
$("#child").parent().attr({id: "div_resizable"});
Upvotes: 2