Jump to content

Trying to add an image into an #id based on page using JS


m_hutchins

Recommended Posts

Hello there,

 

I'm trying to add certain/specific images pages using JS. I'm trying to load the image to the page based on the url. The image is loading,but breaking the layout a bit. This is the code I'm trying/testing with now:

 

var current = window.location.href;

if (
current == "http://www.sjmradio.oiw9.com/industry-solutions/construction.htm" ||
current == "http://www.sjmradio.oiw9.com/industry-solutions/construction.htm/"
){
$('<img class=" class="fade-in" src="/images/headers/industry-construction.jpg').html("#page-header");

}

 

I'm also using some code for other pages to load random images into the same id (of different pages of course) and it works fine. This is the code I'm using for that:

 

//Add your images, we'll set the path in the next step
var images = ['top-header-001.jpg', 'top-header-002.jpg', 'top-header-003.jpg', 'top-header-004.jpg', 'top-header-005.jpg', 'top-header-006.jpg', 'top-header-007.jpg', 'top-header-008.jpg', 'top-header-009.jpg', 'top-header-010.jpg', 'top-header-011.jpg', 'top-header-012.jpg','top-header-013.jpg','top-header-014.jpg','top-header-015.jpg','top-header-016.jpg','top-header-017.jpg','top-header-018.jpg','top-header-019.jpg', 'top-header-020.jpg'];

//Build the img, then do a bit of maths to randomize load and append to a div. Add a touch off css to fade them badboys in all sexy like.
$('<img class=" class="fade-in" src="/images/random-headers/' + images[Math.floor(Math.random() * images.length)] + '">').appendTo('#page-header');

 

Any help would be greatly appreciated!

 

Thanks in advance,

mh

Link to comment
Share on other sites

The first part should be the selector of the element id class to target, so you are telling it to look for '<img class=" class="fade-in" src="/images/headers/industry-construction.jpg' and and insert "#page-header" ???? see the problem here! since img tag is self closing and not a container element like 'div' or 'p' element thats not going to happen, you are getting in a mucking fuddle and doing it the wrong way round.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...