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

224
Vistas
Replace white space with a dash using javascript inside a hyperlink?

I'm trying to format a bunch of auto generated links containing white spaces, into versions where the white spaces have been replaced by "-" (dash) like this:

<a id="replacewhitespace" href="/properties-for-sale in Los Angeles">Los Angeles</a>

Changed into:

<a id="replacewhitespace" href="/properties-for-sale-in-Los-Angeles">Los Angeles</a>

The white space breaks the links.

I'm quite new to javascript, and been trying a bunch of stuff from researching online, but i can't seem to find a way to target the "href" part of a link. Up until now i managed to put the following together, but it only changes "normal" text (seen in the front-end). I need to target the html/href part.

var orignalstring = document.getElementById("replacewhitespace").innerHTML;
var newstring = orignalstring.replace("","-");
document.getElementById("replacewhitespace").innerHTML = newstring;
<li><span class="property-type">Penthouses</span> for sale in <span id="property-area"><a id="replacewhitespace" href="/properties-for-sale in Los Angeles">Los Angeles</a></span></li>

Can anyone help/guide me in the right direction?

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

0

You'll have to first decode the anchor's target (accessed via the href property) with decodeURI, then use replaceAll:

var orignalstring = document.getElementById("replacewhitespace").href;
var newstring = decodeURI(orignalstring).replaceAll(" ","-");
document.getElementById("replacewhitespace").href = newstring;

console.log(newstring)
<li><span class="property-type">Penthouses</span> for sale in <span id="property-area"><a id="replacewhitespace" href="/properties-for-sale in Los Angeles">Los Angeles</a></span></li>

Or more simply:

var elem = document.getElementById("replacewhitespace");
elem.href = elem.href.replaceAll("%20", "-")

console.log(elem.href)
<li><span class="property-type">Penthouses</span> for sale in <span id="property-area"><a id="replacewhitespace" href="/properties-for-sale in Los Angeles">Los Angeles</a></span></li>

If you have multiple links, you can do:

document.querySelectorAll("a[href*=' ']").forEach(e => {
  e.href = e.href.replaceAll("%20", "-");
  console.log(e.href)
})
<li><span class="property-type">Penthouses</span> for sale in <span id="property-area">'
<a id="replacewhitespace" href="/properties-for-sale in Los Angeles">Los Angeles</a>
<a id="replacewhitespace" href="/properties-for-sale in Los Angeles">Los Angeles</a>
</span></li>

about 4 years ago · Juan Pablo Isaza Denunciar

0

  • You don't need to modify the innerHTML, just use Element.getAttribute and Element.setAttribute
  • Use String.prototype.replaceAll or String.prototype.replace using the Global g flag for the RegExp part
  • Find all your elements with space inside a href attribute by using the Attribute [href*=' '] selector:

const elsHrefWithSpaces = document.querySelectorAll("[href*=' ']");

elsHrefWithSpaces.forEach(el => {
  el.setAttribute("href", el.getAttribute("href").replace(/ /g, "-"));
});
<a id="replacewhitespace" href="/properties-for-sale in Los Angeles">Los Angeles</a>  
<br>
<a id="replacewhitespace" href="/some spaces here">Los Spaces</a>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Thanks guys!

Just as the answers were coming in, i managed to do it this way by a little (!!! - spend 2 hours prior to this :-D ) tampering around in JSFiddle:

function replace() {
    var aEl = document.getElementById('replacewhitespace');
    aEl.href = aEl.href.replaceAll('%20', '-');
}


replace();
<a id="replacewhitespace" href="/penthouses for sale in Los Angeles">Los Angeles</a>

Works in the JSFiddle at least - i guess this will do the trick (?!).

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