Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

327
Vistas
Replace existing text (city name) in title with new city name of current page only

I'm publishing multiple pages on WordPress with same contents but city names on titles and contents must be replaced with the current page city like New York, Los Angeles, Sacramento and so on. How do I do this on Javascript

    <script>
        function replaceText() {      
        //on page load, replace #cityName with current city name
        var cityName = document.getElementById("cityName");
        cityName.innerHTML = "New York";
        replaceText();
    
    }
    </script>
    
         <div class="container">
            <div class="row">
                <h1>Our Mental Health Services in <span id="cityName">Benton</span>, KY</h1>
                <h2>Online Counseling and Psychiatry Services in <span 
 id="cityName">Benton</span>, KY</h2>
                
            </div>
        </div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

First, you have two elements with the same ID. By calling getElementById, Javascript will find the first one, and do what you wish with it.

Second, you were calling the function inside her self, so she could never actually be executed.

Lastly, we don't have the whole HTML code, so I'm not sure where where you calling the script, so I've put it as an external file in the snippet.

function replaceText() {      
        //on page load, replace #cityName with current city name
        var cityName = document.getElementById("cityName");
        var cityNameH2= document.getElementById("cityNameH2");
        cityNameH2.innerHTML = cityName.innerHTML = "New York";           
    }
     replaceText();
   
         <div class="container">
            <div class="row">
                <h1>Our Mental Health Services in <span id="cityName">Benton</span>, KY</h1>
                <h2>Online Counseling and Psychiatry Services in <span 
 id="cityNameH2">Benton</span>, KY</h2>
                
            </div>
        </div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda