Receiving backgroud-image attribute of a div

1

I'm trying to get it like this:

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

but it returns empty.

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

I'm missing the wrong command, or not trying to get the class?

I tried several ways and I could not.

Thank you in advance for the help!

    
asked by anonymous 12.06.2014 / 02:15

3 answers

2

With the method below you can get both the attributes defined in 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.

    
29.06.2014 / 22:30
0

I'm not sure what to look for, if you want to save the url in a variable, in this link ! Maybe it will help.

$(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!)
    
30.06.2014 / 05:17
0

To find 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 not or do not want to use jQuery, there's a way to do it with just Javascript.

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 did not realize that you were using WebBrowser , so try to access the Style attribute of the element returned by GetElementById . Example:

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

Now using this property you can search for background-image within String . Source: MSDN HtmlElement.Style Property

    
01.07.2014 / 13:20