I know that there are many similar questions, and I looked through them. This was not enough. So.
I have a situation where I need to duplicate an element and find out the original element from which I cloned, so I do it like this, which is ugly:
<div id="main"> <p>Hello</p> </div>
then
$(somewhere).append( $('#main') .clone() .attr('src', 'main') ));
Now this is very ugly as I was attaching an identifier from the original element that I cloned. In my situation, I don't know if the element has an identifier, so I could create a random one and assign it like this:
var id = $(element).attr('id'); if (!id) { var id = 'id_'+ Math.round(Math.random()*99999999); $(element).attr('id', id); } $(somewhere).append( $(element) .clone() .attr('src', id) ));
My decisions seem very dirty to me, and I have a chance to run into, of course ... so I would like to know any clean way to refer to a DOM element from another element.
UPDATE
So in conclusion, I used jQuery .uniqueId(). and just .attr() item id via .attr() as $(el).attr('src', $(parent).attr('id') );
source share