user5140488
user5140488

Reputation: 83

Add attribute to an object

How can I change the attribute of a tag in some HTML code given as an object?

Currently, if I do:

console.log(gallery.currItem);

I get:

So I then did:

console.log(gallery.currItem.html);

And I get the HTML in console (not as an object, I believe it's just as texy):

<video width="500" height="250" controls><source src="" type=""></video>

But I'm not sure how I would edit the video tag by adding the attribute muted="muted".

I tried:

console.log($(gallery.currItem.html).find('video'));

But this returned an object again. :/

Upvotes: 3

Views: 179

Answers (3)

Sebastian Nette
Sebastian Nette

Reputation: 7812

I assume you are using PhotoSwipe. gallery.currItem.html is not a string but an actual html element. You can just directly edit the attributes of it:

gallery.currItem.html.setAttribute("muted", "muted");

To make sure it's an actual element, if in question, do this:

if(gallery.currItem.html.tagname) {
    gallery.currItem.html.setAttribute("muted", "muted");
} else {
    // append it to the dom or wrap it into jquery and then set the attributes
    gallery.currItem.html = $(gallery.currItem.html).attr("muted", "muted")[0];
}

Upvotes: 1

user5140488
user5140488

Reputation: 83

Answer:

$(gallery.currItem.container.lastChild).attr('muted', 'muted');

Upvotes: 0

jrath
jrath

Reputation: 990

try this.

 $(gallery.currItem.html).attr("muted", "muted");

Upvotes: 0

Related Questions