I’m unable to edit a h1 tag without changing the css. Here is the string without the javascript:
Here is the string with the javascript:
I’m not having this problem elsewhere. THe rest of the code snippets were properly manipulated by the JS code.
Here is the HTML and JS snippets:
document.getElementById('currentContact').innerText = state.translate('test');
<div class="class_Name" id="currentContact">
<h1>test</h1>
</div>
Troubleshooting:
- Tried moving the div to between the tags
- Tried innerHTML, textContents, innerText.
I expect the manipulated string to keep the same styling.
3
Answers
It is because you are replacing the
<h1>
element when you set theinnerText
property. Target and manipulate the<h1>
element inside the<div>
instead, like:try stuffing
document.getElementById(‘currentContact’).innerText = state.translate(‘test’);
to a variable, like let text, then call text.style.color = ‘red’;
maybe I misunderstood your question
Your
currentContact
is the<div>
that contains the<h1>
. If you change theinnerText
ofcurrentContact
, it will completely override the<h1>
.As a solution, you could give your
<h1>
element the idcurrentContact
. or insert a new<h1>
when you assign a new value toinnerHTML
instead ofinnerText
.