-3

The following code takes in an element, taken from the DOM and the second variable is a predefined empty object. The LHS of the final line create an attribute within the attributes object then the RHS fetches the same attribute from the myEl and assigns it to the LHS. or should do. Expected output is new object with attribute id, for example, equals the id attribute of the element passed in.

function getAttributes(myEl, myObj){
    attArray = [
        "tagName",
        "id",
        "name"
    ];

    for (var att in attArray){
        if (myEl.hasOwnProperty(attArray[att])) {
                myObj.attributes = {};
                myObj.attributes.hasOwnProperty(attArray[att]) == myEl.hasOwnProperty(attArray[att]);
        };
    };
};
hipkiss
  • 197
  • 2
  • 19
  • 2
    please add the content of `iterateElement`. – Nina Scholz Jun 30 '16 at 09:47
  • 1
    please add the rest of the code as well – Th0rndike Jun 30 '16 at 09:48
  • 1
    oh no, please use not `Object` as variable name. – Nina Scholz Jun 30 '16 at 09:53
  • Yes I know that. but I'll change it in the question to stop confusion. Production object name = different – hipkiss Jun 30 '16 at 09:54
  • what is `myEl`, `myObj`? and what does the comparison in the last line of the if clause? – Nina Scholz Jun 30 '16 at 09:57
  • 1
    Your code doesn't `return` anything. Do you refer to that dead comparison? Please show us how you call this function, and especially what arguments you pass in, then tell us what the expected result is. – Bergi Jun 30 '16 at 09:57
  • @Bergi, Thanks. Edited the question. If I've explained it properly now, can people please remove the downvotes. – hipkiss Jun 30 '16 at 10:09
  • 1
    Uh, `==` is not an assignment and `.hasOwnProperty()` is a check, not a property creation or fetch? It sounds like you are looking for a simple `myObj.attributes[attArray[att]] = myEl[attArray[att]];` (and you should move the `myObj.attributes = {};` outside of the loop and you [should not use `for…in` enumerations on arrays](https://stackoverflow.com/q/500504/1048572)) – Bergi Jun 30 '16 at 10:24
  • @Bergi, can you stick that as an answer - you fixed it for me. Thank you so much. Seems so trivial now. – hipkiss Jun 30 '16 at 10:33
  • Trivial enough for the question to be closed instead of answered, imo :-) – Bergi Jun 30 '16 at 11:16
  • I tried to delete it but that didn't work. I don't know how you close a question. – hipkiss Jun 30 '16 at 11:17

1 Answers1

1

If you're trying to figure out if element has an attribute from the list, you can use .hasAttribute(attributeName) like this:

iterateElement.hasAttribute(attArray[i]) // returns true or false