Let's say I have a following code:
function createElement(element) {
$('#container').append(element);
}
createElement('<div id='abc'>Some content</div>');
I'd like to let the function know which element id
it creating (hopefully without parsing of the element
string) to manipulate with just created element such as setting the position.
Is there any solution?
function createElement(element) {
var node = $(element);
$('#container').append(node);
alert(node.attr('id'));
}
createElement('<div id="abc">Some content</div>');
See the working code at:
Try using .appendTo()
in this context,
function createElement(element) {
return $(element).appendTo('#container');
}
console.log(createElement('<div id="abc">Some content</div>').attr('id'))
You can use this.id to get the id like this:
function createElement(element) {
var id = $(element).attr('id');//and do your stuff with the id
$('#container').append(element);
}
createElement('<div id="abc">Some content</div>');
just do:
function createElement(element) {
$('#container').append(element);
return $(element).attr("id");
}
console.log(createElement('<div id="abc">Some content</div>'));
Demo:: jsFiddle
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.