Skip to content

Commit

Permalink
Remove innerHTML assignments, non-API docs (mdn#34917)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena authored Jul 19, 2024
1 parent 314bf48 commit 1d306a4
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 57 deletions.
10 changes: 5 additions & 5 deletions files/en-us/games/techniques/control_mechanisms/other/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ Leap.loop({
horizontalDegree = Math.round(hand.roll() * toDegrees);
verticalDegree = Math.round(hand.pitch() * toDegrees);
grabStrength = hand.grabStrength;
output.innerHTML =
`Leap Motion: <br />` +
` roll: ${horizontalDegree}° <br />` +
` pitch: ${verticalDegree}° <br />` +
` strength: ${grabStrength}`;
output.innerText = `Leap Motion:
roll: ${horizontalDegree}°
pitch: ${verticalDegree}°
strength: ${grabStrength}
`;
},
});
```
Expand Down
6 changes: 3 additions & 3 deletions files/en-us/learn/accessibility/css_and_javascript/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ We only do the validation when the form is submitted — this is so that we don'
form.onsubmit = validate;

function validate(e) {
errorList.innerHTML = "";
errorList.textContent = "";
for (let i = 0; i < formItems.length; i++) {
const testItem = formItems[i];
if (testItem.input.value === "") {
Expand All @@ -277,7 +277,7 @@ function validate(e) {
}
}

if (errorList.innerHTML !== "") {
if (errorList.hasChildNodes()) {
e.preventDefault();
}
}
Expand All @@ -287,7 +287,7 @@ function validate(e) {
Real form validation would be much more complex than this — you'd want to check that the entered name actually looks like a name, the entered age is actually a number and is realistic (e.g. nonnegative and less than 4 digits). Here we've just implemented a simple check that a value has been filled in to each input field (`if (testItem.input.value === '')`).

When the validation has been performed, if the tests pass then the form is submitted. If there are errors (`if (errorList.innerHTML !== '')`) then we stop the form submitting (using [`preventDefault()`](/en-US/docs/Web/API/Event/preventDefault)), and display any error messages that have been created (see below). This mechanism means that the errors will only be shown if there are errors, which is better for usability.
When the validation has been performed, if the tests pass then the form is submitted. If there are errors (`if (errorList.hasChildNodes())`) then we stop the form submitting (using [`preventDefault()`](/en-US/docs/Web/API/Event/preventDefault)), and display any error messages that have been created (see below). This mechanism means that the errors will only be shown if there are errors, which is better for usability.

For each input that doesn't have a value filled in when the form is submitted, we create a list item with a link and insert it in the `errorList`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function updateValue(select, index) {
const optionList = select.querySelectorAll(".option");

nativeWidget.selectedIndex = index;
value.innerHTML = optionList[index].innerHTML;
value.textContent = optionList[index].textContent;
highlightOption(select, optionList[index]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function updateValue(select, index) {
optionList[index].setAttribute("aria-selected", "true");

nativeWidget.selectedIndex = index;
value.innerHTML = optionList[index].innerHTML;
value.textContent = optionList[index].textContent;
highlightOption(select, optionList[index]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ function updateValue(select, index) {
nativeWidget.selectedIndex = index;

// We update the value placeholder accordingly
value.innerHTML = optionList[index].innerHTML;
value.textContent = optionList[index].textContent;

// And we highlight the corresponding option of our custom control
highlightOption(select, optionList[index]);
Expand Down Expand Up @@ -1571,7 +1571,7 @@ function updateValue(select, index) {
const optionList = select.querySelectorAll(".option");

nativeWidget.selectedIndex = index;
value.innerHTML = optionList[index].innerHTML;
value.textContent = optionList[index].textContent;
highlightOption(select, optionList[index]);
}

Expand Down Expand Up @@ -1708,7 +1708,7 @@ function updateValue(select, index) {
optionList[index].setAttribute("aria-selected", "true");

nativeWidget.selectedIndex = index;
value.innerHTML = optionList[index].innerHTML;
value.textContent = optionList[index].textContent;
highlightOption(select, optionList[index]);
}
```
Expand Down Expand Up @@ -1902,7 +1902,7 @@ function updateValue(select, index) {
optionList[index].setAttribute("aria-selected", "true");

nativeWidget.selectedIndex = index;
value.innerHTML = optionList[index].innerHTML;
value.textContent = optionList[index].textContent;
highlightOption(select, optionList[index]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ select.addEventListener('change', () => {
});

function createCalendar(days, choice) {
list.innerHTML = '';
list.textContent = "";
h1.textContent = choice;
for (let i = 1; i <= days; i++) {
const listItem = document.createElement('li');
Expand Down Expand Up @@ -530,7 +530,7 @@ select.addEventListener("change", () => {
});
function createCalendar(days, choice) {
list.innerHTML = "";
list.textContent = "";
h1.textContent = choice;
for (let i = 1; i <= days; i++) {
const listItem = document.createElement("li");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ If you get really stuck, press "Show solution" to see a solution.
</p>
<textarea id="code" class="playable-code" style="height: 300px;width: 95%">
let output = document.querySelector('.output');
output.innerHTML = '';
output.textContent = "";

// let i = 10;

Expand Down Expand Up @@ -682,7 +682,7 @@ solution.addEventListener("click", function () {
});

let jsSolution = `const output = document.querySelector('.output');
output.innerHTML = '';
output.textContent = "";
let i = 10;
Expand Down
16 changes: 8 additions & 8 deletions files/en-us/learn/javascript/first_steps/arrays/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ Let's return to the example we described earlier — printing out product names
const list = document.querySelector('.output ul');
const totalBox = document.querySelector('.output p');
let total = 0;
list.innerHTML = '';
totalBox.textContent = '';
list.textContent = "";
totalBox.textContent = "";
// number 1
'Underpants:6.99'
'Socks:5.99'
Expand Down Expand Up @@ -360,8 +360,8 @@ solution.addEventListener("click", () => {
const jsSolution = `const list = document.querySelector('.output ul');
const totalBox = document.querySelector('.output p');
let total = 0;
list.innerHTML = '';
totalBox.textContent = '';
list.textContent = "";
totalBox.textContent = "";
const products = [
'Underpants:6.99',
Expand Down Expand Up @@ -489,7 +489,7 @@ const list = document.querySelector('.output ul');
const searchInput = document.querySelector('.output input');
const searchBtn = document.querySelector('.output button');

list.innerHTML = '';
list.textContent = "";

const myHistory = [];
const MAX_HISTORY = 5;
Expand All @@ -501,7 +501,7 @@ searchBtn.onclick = () => {

// empty the list so that we don't display duplicate entries
// the display is regenerated every time a search term is entered.
list.innerHTML = '';
list.textContent = "";

// loop through the array, and display all the search terms in the list
for (const itemText of myHistory) {
Expand Down Expand Up @@ -584,7 +584,7 @@ const jsSolution = `const list = document.querySelector('.output ul');
const searchInput = document.querySelector('.output input');
const searchBtn = document.querySelector('.output button');
list.innerHTML = '';
list.textContent = "";
const myHistory = [];
const MAX_HISTORY = 5;
Expand All @@ -596,7 +596,7 @@ searchBtn.onclick = () => {
// empty the list so that we don't display duplicate entries
// the display is regenerated every time a search term is entered.
list.innerHTML = '';
list.textContent = "";
// loop through the array, and display all the search terms in the list
for (const itemText of myHistory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ Think about how you could test whether the message in each case is a Christmas m

<textarea id="code" class="playable-code" style="height: 290px; width: 95%">
const list = document.querySelector('.output ul');
list.innerHTML = '';
list.textContent = "";
const greetings = ['Happy Birthday!',
'Merry Christmas my love',
'A happy Christmas to all the family',
Expand Down Expand Up @@ -314,7 +314,7 @@ solution.addEventListener("click", () => {
});

const jsSolution = `const list = document.querySelector('.output ul');
list.innerHTML = '';
list.textContent = "";
const greetings = [
'Happy Birthday!',
'Merry Christmas my love',
Expand Down Expand Up @@ -411,7 +411,7 @@ In this exercise, we have the names of cities in the United Kingdom, but the cap

<textarea id="code" class="playable-code" style="height: 250px; width: 95%">
const list = document.querySelector('.output ul');
list.innerHTML = '';
list.textContent = "";
const cities = ['lonDon', 'ManCHESTer', 'BiRmiNGHAM', 'liVERpoOL'];

for (const city of cities) {
Expand Down Expand Up @@ -483,7 +483,7 @@ solution.addEventListener("click", function () {
});

const jsSolution = `const list = document.querySelector('.output ul');
list.innerHTML = '';
list.textContent = "";
const cities = ['lonDon', 'ManCHESTer', 'BiRmiNGHAM', 'liVERpoOL'];
for (const city of cities) {
Expand Down Expand Up @@ -586,7 +586,7 @@ We'd recommend doing it like this:

<textarea id="code" class="playable-code" style="height: 285px; width: 95%">
const list = document.querySelector('.output ul');
list.innerHTML = '';
list.textContent = "";
const stations = ['MAN675847583748sjt567654;Manchester Piccadilly',
'GNF576746573fhdg4737dh4;Greenfield',
'LIV5hg65hd737456236dch46dg4;Liverpool Lime Street',
Expand Down Expand Up @@ -662,7 +662,7 @@ solution.addEventListener("click", function () {
});

const jsSolution = `const list = document.querySelector('.output ul');
list.innerHTML = '';
list.textContent = '';
const stations = ['MAN675847583748sjt567654;Manchester Piccadilly',
'GNF576746573fhdg4737dh4;Greenfield',
'LIV5hg65hd737456236dch46dg4;Liverpool Lime Street',
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/learn/mathml/first_steps/scripts/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ scriptedElements.forEach((scripted) => {
});
document.getElementById("clearOutput").addEventListener("click", () => {
clearHighlight();
outputDiv.innerHTML = "";
outputDiv.textContent = "";
});
```

Expand Down
4 changes: 2 additions & 2 deletions files/en-us/learn/mathml/first_steps/text_containers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ tokenElements.forEach((token) => {
});
document.getElementById("clearOutput").addEventListener("click", () => {
clearHighlight();
outputDiv.innerHTML = "";
outputDiv.textContent = "";
});
```

Expand Down Expand Up @@ -413,7 +413,7 @@ tokenElements.forEach((token) => {
});
document.getElementById("clearOutput").addEventListener("click", () => {
clearHighlight();
outputDiv.innerHTML = "";
outputDiv.textContent = "";
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function change(event) {

switch (event.target.id) {
case "year":
yearOut.innerHTML = event.target.value;
yearOut.textContent = event.target.value;
break;
default:
return;
Expand Down
8 changes: 4 additions & 4 deletions files/en-us/web/accessibility/aria/roles/alert_role/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ Using JavaScript, you can dynamically change the content _inside_ the element wi

```js
// clear the contents of the container
document.getElementById("alertContainer").innerHTML = "";
document.getElementById("alertContainer").textContent = "";
// inject the new alert message
document.getElementById("alertContainer").innerHTML =
document.getElementById("alertContainer").textContent =
"Your session will expire in " + expiration + " minutes";
```

Expand Down Expand Up @@ -102,9 +102,9 @@ However, make sure that the container is not hidden using `display:none`, as thi

```js
// clear the contents of the container
document.getElementById("hiddenAlertContainer").innerHTML = "";
document.getElementById("hiddenAlertContainer").textContent = "";
// inject the new alert message
document.getElementById("hiddenAlertContainer").innerHTML =
document.getElementById("hiddenAlertContainer").textContent =
"All items were removed from your inventory.";
```

Expand Down
4 changes: 2 additions & 2 deletions files/en-us/web/css/@container/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ You can then use the `@container` at-rule to apply styles to the element with th
```js hidden
const post = document.querySelector(".post");
const span = document.createElement("span");
span.innerHTML = ".post width: " + post.clientWidth + "px";
span.textContent = ".post width: " + post.clientWidth + "px";
post.parentNode.insertBefore(span, post.nextSibling);
// update on resize
window.addEventListener("resize", () => {
span.innerHTML = ".post width: " + post.clientWidth + "px";
span.textContent = ".post width: " + post.clientWidth + "px";
});
```

Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/css/_colon_scope/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ This example demonstrates using the `:scope` pseudo-class in JavaScript. This ca
const context = document.getElementById("context");
const selected = context.querySelectorAll(":scope > div");

document.getElementById("results").innerHTML = Array.prototype.map
document.getElementById("results").textContent = Array.prototype.map
.call(selected, (element) => `#${element.getAttribute("id")}`)
.join(", ");
```
Expand Down
4 changes: 3 additions & 1 deletion files/en-us/web/css/length/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ inputElem.addEventListener("change", () => {
const result = document.createElement("div");
result.className = "result";
result.style.width = inputElem.value;
result.innerHTML = `<code>width: ${inputElem.value}</code>`;
const code = document.createElement("code");
code.textContent = `width: ${inputElem.value}`;
result.appendChild(code);
resultsDiv.appendChild(result);

inputElem.value = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ const node = document.getElementById("formula");
MathJax.typesetClear([node]);
// Throws in older ECMAScript versions (ES2016 and earlier)
// SyntaxError: malformed Unicode character escape sequence
node.innerHTML = String.raw`$\underline{u}$`;
node.textContent = String.raw`$\underline{u}$`;
MathJax.typesetPromise([node]);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ window.onload = () => {
p.addEventListener(
"input",
() => {
c.innerHTML = p.value;
c.textContent = p.value;
v.playbackRate = p.value;
},
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function renderPastPeriods() {
}

// Clear the list of past periods, since we're going to re-render it.
pastPeriodContainer.innerHTML = "";
pastPeriodContainer.textContent = "";

const pastPeriodHeader = document.createElement("h2");
pastPeriodHeader.textContent = "Past periods";
Expand Down Expand Up @@ -289,7 +289,7 @@ function renderPastPeriods() {
if (periods.length === 0) {
return;
}
pastPeriodContainer.innerHTML = "";
pastPeriodContainer.textContent = "";
pastPeriodHeader.textContent = "Past periods";
periods.forEach((period) => {
const periodEl = document.createElement("li");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ const output = document.getElementById("output");
function assertEq(msg, got, expected) {
const result =
got === expected
? `SUCCESS! Got: ${got}<br>`
: `FAIL!<br>Got: ${got}<br>Expected: ${expected}<br>`;
output.innerHTML += `Testing ${msg}: ${result}`;
? `SUCCESS! Got: ${got}\n`
: `FAIL!\nGot: ${got}\nExpected: ${expected}\n`;
output.innerText += `Testing ${msg}: ${result}`;
}

assertEq("WebAssembly.Global exists", typeof WebAssembly.Global, "function");
Expand Down
Loading

0 comments on commit 1d306a4

Please sign in to comment.