Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
DOM manipulation with querySelector()

I want to pick the url value of the style in this div

<div 
class="w-18 h-18 sm:w-20 sm:h-20 bg-gray-200 mx-auto bg-center bg-cover border border-gray-400 rounded-ch" 
style="background-image: URL('https://d14u0p1qkech25.cloudfront.net/club_8702_65d827dd-03b9-4cce-8876-8bd33f306460_thumbnail_250x250')"
></div>

I was able to get the div with

document.querySelector('rounded-ca')

Then I used the .style property on the div to pick the style but it's only returning an object like this

{ "0": "background-image" }

How can I then get the url value assigned to background-image?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try something like this:

div = document.getElementsByClassName("rounded-ch")[0]
style = div.getAttribute("style")
link = style.replace("URL('", "").slice(0, -2)
console.log(link)
<div 
class="w-18 h-18 sm:w-20 sm:h-20 bg-gray-200 mx-auto bg-center bg-cover border border-gray-400 rounded-ch" 
style="background-image: URL('https://d14u0p1qkech25.cloudfront.net/club_8702_65d827dd-03b9-4cce-8876-8bd33f306460_thumbnail_250x250')"
></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

The style property of the div holds properties than you described. You can access each style property, like background-image, by accessing it's camelCased property. For example: style.backgroundImage.

Then check the string for the content between the url(" and ") characters.

const div = document.querySelector('.rounded-ch');
const { backgroundImage } = div.style;
const pattern = /(?<=url\(").*(?="\))/g;
const [url] = backgroundImage.match(pattern);
console.log(url);
<div 
class="w-18 h-18 sm:w-20 sm:h-20 bg-gray-200 mx-auto bg-center bg-cover border border-gray-400 rounded-ch" 
style="background-image: URL('https://d14u0p1qkech25.cloudfront.net/club_8702_65d827dd-03b9-4cce-8876-8bd33f306460_thumbnail_250x250')"
></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Check out the documentation for getComputedStyle:

let element = document.querySelector('rounded-ca');
let compStyles = window.getComputedStyle(element);
console.log(compStyles.getPropertyValue('background-image'));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda