0

How is it possible to return a string from a JavaScript Code into a HTML attribute like in the following example?

<div class="xx" animation="yy" animate-duration="<spript>Code goes here<script>" ...>
</div>

I can not get it right, is there a solution?

2
  • have you tried element.setAttribute()? Commented Feb 14, 2016 at 14:04
  • no, how would this work? Commented Feb 14, 2016 at 14:06

2 Answers 2

1
<div class="xx" id="id1" animation="yy"></div>
<script>
document.getElementById("id1").setAttribute("animate-duration", "your value");
</script>

The id of the above <div> tag is id1. So document.getElementById("id1").setAttribute("animate-duration", "your value"); selects the <div> tag and sets it's animate-duration value to whatever you set.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't think your example will work because the script tags will only be treated as a string. I suggest you use Element.setAttribute(attribute name, attribute value) if you only want to set the attribute of the element.

<div class="xx"></div>
<script>document.querySelector(".xx").setAttribute("animate-duration", "value");</script>

See the following for more info:

https://developer.mozilla.org/en/docs/Web/API/Element/setAttribute

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.