Is there a function for adding a word in front of another in an HTML element like a p tag using JavaScript

Viewed 73

Say I have a select element with some options as shown below:

 <select class="form-control" id="number-selector">
     <option>One</option>
     <option>Two</option>
     <option>Three</option>
     <option>Four</option>
     <option>Five</option>
 </select>

And a paragraph element

<p id="sh">Sheep</p>

What I am trying to do is have any of the options selected from the select element added to the front of the word in the p tag, so I can have One Sheep if one is selected, Two Sheep if Two is selected, and so on.

This is what I have tried but it just replaces the existing word in the p tag and other things that I have tried like insertBefore do not also work.

function getSelectedNumber() {
    let selectedNumber = document.getElementById('number-selector');
    let numb = selectedNumber.options[selectedNumber.selectedIndex];

    let newText = document.getElementById('sh');
    newText.innerHTML = "<span> " + numb.innerText + "</span> "+ newText.innerText;
}

This just keeps adding every new selection instead of replacing just the previously selected value so I get: "One Sheep" if I select One first, then if I select Two next I get "Two One Sheep" and so on. I want the previously selected option to be replaced by a new selection.

And for multiple elements, say:

<p id="sh">Sheep</>
<p id="of">Offspring</>
<p id="mu">Music</>

The JavaScript code:

function getSelectedNumber() {
    let arr = ['sh','of','mu'];
    let selectedNumber = document.getElementById('number-selector');
    let numb = selectedVersion.options[selectedVersion.selectedIndex];

    for(let i = 0; i < arr.length; i++){
        let newText = document.getElementById(arr[i]);
        newText.innerHTML = "<span> " + numb.innerText + "</span>" ;
    }
}

But the code above just replaces the existing words in the p tags

3 Answers

You can try like this by looking for prefix Element first and replace it's value. if it's not found then add that prefix element. Or

the other way is keeping that element in DOM always like <p id="sh"><span id="prefix"></span>Sheep</p>

Updated: modified code to support updating multiple elements.

function getSelectedNumber() {
    let arr = ['sh','of','mu'];
    let numb = document.getElementById('number-selector');

    for(let i = 0; i < arr.length; i++){
          const ele = document.querySelector(`#${arr[i]} .prefix`);
        if (ele) {
          ele.innerText = numb.value;
        } else {
          let newText = document.getElementById(arr[i]);
          newText.innerHTML = "<span class='prefix'> " + numb.value + "</span> "+ newText.innerText;
        }
    }
}
getSelectedNumber()
<select class="form-control" id="number-selector" onchange="getSelectedNumber();">
     <option>One</option>
     <option>Two</option>
     <option>Three</option>
     <option>Four</option>
     <option>Five</option>
 </select>
 
<p id="sh">Sheep</p>
<p id="of">Offspring</p>
<p id="mu">Music</p>

You can use add an empty span tag as a slot and just modify it with the value selected from the dropdown

<select class="form-control" id="number-selector" onchange="getSelectedNumber()">
     <option>One</option>
     <option>Two</option>
     <option>Three</option>
     <option>Four</option>
     <option>Five</option>
 </select>
<p id="sh"><span id="slot"></span>Sheep</p>
function getSelectedNumber() {
    let selectedNumber = document.getElementById('number-selector');
    let numb = selectedNumber.options[selectedNumber.selectedIndex];

    let newText = document.getElementById('slot');
    newText.innerHTML =  numb.innerText+' '
}
getSelectedNumber()

codepenlink

What is happening with this code: when you write this part of the code:

newText.innerHTML = `<span> ${numb.innerText} </span> ${newText.innerText}`;

the ${} is just a sugar syntax to replace the concat with +

The newText.innerText will get the previos state. Example, if in this turn you have the option four Sheep. Now when you click in option one the newText.innerText will get the old value that was four Sheep and then will contruct this string:

<span> ${numb.innerText} </span> ${newText.innerText};

That means:

  • numb.innerText // one
  • newText.innerText // four Sheep
  • result: one four Sheep
  • The next turn the newText.innerText will be one four Sheep and so on

Possible Solution:clean the newText.innerText with the value Sheep: newText.innerText='Sheep'; or fix the value Sheep in the newText:

newText.innerHTML = `<span> ${numb.innerText} </span> Sheep`;

var selectedNumber = document.getElementById('number-selector');

function getSelectedNumber() {
    let numb = selectedNumber.options[selectedNumber.selectedIndex];

    let newText = document.getElementById('sh');
    //First option:
    //newText.innerText='Sheep';
    //newText.innerHTML = `<span> ${numb.innerText} </span> ${newText.innerText}`;
    // second option
    newText.innerHTML = `<span> ${numb.innerText} </span> Sheep`;
}
selectedNumber.onchange = getSelectedNumber;
 <select class="form-control" id="number-selector">
        <option>One</option>
        <option>Two</option>
        <option>Three</option>
        <option>Four</option>
        <option>Five</option>
</select>

<p id="sh">Sheep</p>

Related