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

125
Vistas
Obtener atributo de cadena HTML (sin jQuery)

Estoy tratando de extraer un atributo de imagen de un HTML que se extrae como una cadena.

Intentar usar https://stackoverflow.com/a/40311944/2981404 devuelve:

TypeError: Cannot read properties of undefined (reading 'slice')

Mi código es el siguiente:

 const html[0] = '<img src="/img/image-example.png" class="image" title="What Im looking for..." alt="This is an example image">' const startFromTitle = html[0].slice( html[0].search('title') ) const title = startFromTitle.slice(5, startFromTitle.search(' ') - 1) console.log(title) // expected "What Im looking for..."

Me encantaría usar jQuery, pero en este proyecto no puedo.

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

0

Debe declarar html como const primero y luego asignar la cadena html a html[0].

Y luego puede eliminar title=" de la cadena ( withoutTitle ) y luego buscar '"' para encontrar el final del atributo.

 const html = []; html[0] = '<img src="/img/image-example.png" class="image" title="What Im looking for..." alt="This is an example image">'; const startFromTitle = html[0].slice( html[0].search('title') ); const withoutTitle = startFromTitle.slice( 7 ); const title = withoutTitle.slice(0, withoutTitle.search('"')); console.log(title);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Puede lograr esto creando un elemento div temporal en DOM :

 const htmlStr = '<img src="/img/image-example.png" class="image" title="What Im looking for..." alt="This is an example image">'; var tmpDiv = document.createElement('div'); tmpDiv.innerHTML = htmlStr; console.log(tmpDiv.querySelector('.image').getAttribute('title'));

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