Setting background-image using jQuery CSS property
You probably want this (to make it like a normal CSS background-image declaration):
$('myObject').css('background-image', 'url(' + imageUrl + ')');
Changing background-image with jQuery CSS property is not working
There are two things you need to correct in this line of code:
$('jumbotron').css('background-image', 'url(' + /images/bg_v3.png + ')');
First, the selector $('jumbotron')
is not going to select any element. It returns null
, hence calling the .css
function will cause an error. If your targeting element has ID = 'jumbotron'
, then the selector should be changed to $('#jumbotron')
. If that element has CSS class of .jumbotron
, then the selector should be $('.jumbotron')
.
Secondly, your 'url(' + /images/bg_v3.png + ')'
part also causes another error. This should be 'url(/images/bg_v3.png)'
.
Set background image in CSS using jquery
Try this:
<div class="rmz-srchbg">
<input type="text" id="globalsearchstr" name="search" value="" class="rmz-txtbox">
<input type="submit" value=" " id="srchbtn" class="rmz-srchico">
<br style="clear:both;">
</div>
<script>
$(function(){
$('#globalsearchstr').on('focus mouseenter', function(){
$(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat");
});
});
</script>
jquery not changing css background-image property
You need to remove the semi-colon(;) after declaring the pixels.
div.css('background', 'url(' + images[i] + ') no-repeat 0px 0px');
Working example : https://jsfiddle.net/DinoMyte/ez8cgsx7/8/
Jquery Dynamically Replace Background Image
That isn't strange that Chrome set your background-image
style unticked, because jquery set background-image as inline style of the element that always has a higher priority than styles from your css file.
Probably, you change old file with a new file, while they have the same name and Chrome don't update background, because he has old file with given name in its cache. You should load file with a new name and reset background-image.
Changing background image using jquery
You need to include the url()
part of the background-image
property.
$('#full').css('background-image','url(img/1.jpg)');
Jquery change background size AND image src
Setting the background style twice overwrites it, so only the last one will stick, you'll need to use valid CSS properties, like background-image
and background-size
to change them seperately :
$(".clickableimg").on('click', function() {
var choosenpic = this.id;
$("#image").val(choosenpic);
$("#preview").css({
'background-image' : 'url(backgrounds/'+choosenpic+')',
'background-size' : '50%'
});
});
Can't change background image in body using jQuery
working example just wrap your code inside document.ready funtion
$(function(){ $("body").css('background-image','url(https://recruiterflow.com/blog/wp-content/uploads/2017/10/stackoverflow.png)');});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><html lang="pl"> <head> <meta charset="UTF-8">
<link rel="stylesheet" href="styles.css"> <Title> Moja firma </Title> </head> <body>
</body> </html>
Related Topics
Show and Hide Divs At a Specific Time Interval Using Jquery
Referenceerror: $ Is Not Defined
Html ≪Select≫ Selected Option Background-Color CSS Style
Uploading 'Canvas' Image Data to the Server
How to Make an HTML Text Box Show a Hint When Empty
React.Js: Set Innerhtml VS Dangerouslysetinnerhtml
Prevent Typing Non-Numeric in Input Type Number
How to Dynamically Create CSS Class in JavaScript and Apply
Jqgrid Does Not Render Correctly in Chrome/Chrome Frame
Check Element CSS Display With JavaScript
Is There Any Cross-Browser JavaScript For Making Vh and Vw Units Work
JavaScript Array: Get "Range" of Items
Why Is Using "For...In" For Array Iteration a Bad Idea