Forum Moderators: open
Thanks.
I like your idea of using the onClick event to pass an argument. How about passing this?
onClick="myFunction(this)"
Your function will recieve a reference to whatever object was clicked as an argument. This will work for all objects, not just images. Now you can read the properties of the object:
myFunction(obj) {
alert(obj.src);
alert(obj.border);
alert(obj.alt);
}
You can now reconstruct a string for an HTML tag that has the same properties as the one that was clicked. The order of the attributes in your string may be different, but does that really matter?
I'd do it like that if I were you, but I'm now going to show how to take things a step further...
What if you want to make sure you don't miss any properties? It's possible to loop though every property for an object like this:
myFunction(obj) {
for (var prop in obj) {
str = str + prop + " = " + obj[prop] + "\n";
}
alert(str);
}
All I've done there is build a string listing the property name, and equals sign, and the property value. The "\n" adds a newline character. You can do whatever string building you like to build your HTML tag. Warning: there are a zillion properties you probably never knew existed, and many don't have valid attributes in HTML!