Receive backgroud-image attribute from a div

Asked

Viewed 522 times

1

I’m trying to get paid like this:

var obj = webBrowser.Document.GetElementById("senderscore").GetAttribute("background-image")

but it returns empty.

The field background-image of div contains a url. And belongs to a class.

I’m wrong for passing the wrong command, or for not trying to take it by the class?

I tried several ways and I couldn’t.

Thanks in advance for the help!

  • Have you tried harvesting there instead of Getattribute, Addclass ? And do the class in css and declare it directly in div.

  • I’m trying to get the image url and not add Érik

  • background-image wouldn’t be the element’s css, within the style attribute? Or background-image is a custom attribute name?

  • Thomas, background-image is inside the style attribute

3 answers

2

With the method below you can get both the attributes defined in the CSS and the attributes calculated by the browser.

var elemento = document.getElementById("senderscore");
var estilo;
if (window.getComputedStyle) {
    estilo = getComputedStyle(elemento)
} else {
    estilo = elemento.currentStyle
}
var obj = estilo.getPropertyValue("background-image");   
alert(obj);

I hope it helps.

  • Thanks Isaac for the reply. But I’m trying to get the background-image link through a Webbrowser.

0

To fetch the value of a css property of a given element using jQuery, we can use the function .css().

//Aqui retorna o valor do background-image
$('#id').css('background-image');
//Enquanto aqui o background-image vai receber o valor 'none'
$('#id').css('background-image', 'none');

Now, if you can’t or don’t want to use jQuery, there is a way to do this with Javascript only.

var element, style, backgroundimage;
element = document.getElementById('id');
style = window.getComputedStyle(element);
backgroundimage = style.getPropertyValue('background-image');

Source: Soen: Get a CSS value with Javascript

@EDIT

I didn’t realize you were using a WebBrowser, so try to access the attribute Style of the element returned by GetElementById. Example:

var obj = webBrowser.Document.GetElementById("senderscore").Style

Ai using this property you can search for the background-image within the String. Source: MSDN Htmlelement.Style Property

  • Thanks Marciano for the reply. But I’m trying to get the background-image link through a Webbrowser.

  • @Victordelicoli took a look at my answer again, I edited it.

0

I’m not sure what you’re looking for if you want to save the url in a variable, at this link! Maybe it’ll help you.

$(function() {
     $("#senderscore").each(function() {
        var bg = $(this).css('background-image');
        bg = bg.replace('url(','').replace(')','');
        alert(bg);//alert para mostrar como a url fica armazenada na variável bg
    });
});

//requer jquery 1.9.1 (onLoad!)
  • Thanks Lauro for the reply. But I’m trying to get the background-image link through a Webbrowser.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.