Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Update or change text within elements in a div using JavaScript and DOM

Okay, so on my webpage I am trying to update the text within a div contained within these span elements.

<div class="output-1-2-1-1">
 <p id="yourNameDisplay">Person</p>
</div>
<div class="output-1-2-1-2">
 <p>Born in <span id="yourMonthDisplay">February</span></p>
</div>

My current method of doing this is as follows. Input is registered from textboxes using these statements in a function after a button is clicked. I have checked and the id "yourMonth" does match the input box, so that's not the issue.

var yourMonth = document.getElementById("yourMonth").value;

And then the variable is then displayed by calling the element ID in the same function below.

document.getElementById("yourMonthDisplay").innerHTML = yourMonth;

But for some reason, the variables aren't showing up in the text after the button is clicked. I have checked to make sure that they are there using a completely separate div in another location and they pop up there fine. I called them and displayed them using the same method and they work fine, as follows:

document.getElementById("output5").innerHTML = yourMonth;

Is there an alternate method you could recommend for displaying these variables that is better?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are using innerHTML to put pure text in a span tag. Should work, but perhaps try innerText instead.

document.getElementById("output5").innerText = yourMonth;

(as mentioned before in the comments, its good to provide more context in your original question)

about 4 years ago · Juan Pablo Isaza Report

0

your event is triggert after click on the button? if yes, then you get the value from the input field. can you catch the value from the input field? if yes then you can put the new value inside the span tag with the selector yourMonthDisplay and not output5. you have the wrong selector.

and the code will work.

function swapMonth() {
    let yourMonth = document.getElementById("yourMonth").value;
    document.getElementById("yourMonthDisplay").innerHTML = yourMonth;  
}
<input id="yourMonth">
<button onclick="swapMonth(this)">swap</button>

<div class="output-1-2-1-1">
    <p id="yourNameDisplay">Person</p>
</div>

<div class="output-1-2-1-2">
     <p>Born in <span id="yourMonthDisplay">February</span></p>
</div>
  
  

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!