0

I'm working on my company's webpage. This is my first serious webpage and lots of doubts came at each line. Now I'm working on a News loading system, and I've one that can't solve:

I'm loading my news with jquery load, attaching elements to previously defined vars:

var newArticle = $('<a href="'+path + i +'.html"><div id="news-article"></div></a>');

var newArticleChild = newArticle.children(); //'cause i want to load articles inside #news-article, not <a></a>

var image = $('<div></div>');
var title = $("<div></div>");
var tag = $("<div></div>");

$(image).load(path + i + ".html #main-image");
$(title).load(path + i + ".html h1");
$(tag).load(path + i + ".html #tag");       

$("#news").append(newArticle); //Apend articles parent to an existing page element

newArticleChild.append(image);
newArticleChild.append(title);
newArticleChild.append(tag);

HTML --------------

<div id="main-image" style="background-image: url(../../assets/images/NewsBackgroundTry.png);
"></div>

I don't really like this because it creates emty divs with another div inside, but this is not a mistake itself.

I don't really need to append image to my page, because what i want from it is it's style, that contains the path to the image in css. I'm doing this this way because I need to apply background-size: cover; style to that image.

My question is, how i can get the background-image style from image variable? I've tried lots of things and looked at as many posts as i found about it, but i can't get this value...

If the solution prevents from appending image to page, it would be much better.

Thanks everyone, Abel!

Java_User
  • 1,303
  • 3
  • 27
  • 38
sajunt4
  • 73
  • 3
  • Three Ajax calls to get content from the same page is a little extreme. Make a single get request and pull out the three elements instead of making 3 ajax calls. – epascarello Oct 14 '15 at 13:17
  • Does this answer your question? [Get URL from background-image Property](https://stackoverflow.com/questions/6397529/get-url-from-background-image-property) – Jason C Sep 08 '20 at 16:25

1 Answers1

0

you could use var bgImg = image.css('background-image'); to get that css property and then replace the nasty url('') part to get just the image path.

Andrei C
  • 768
  • 9
  • 21
  • Well, I know how it should be done, but I can't manage it to work on that exact case. Thank you.. – sajunt4 Oct 14 '15 at 14:04