jQuery changing style of HTML element

JqueryHtmlCss

Jquery Problem Overview


I have a list on HTML

<div id="header" class="row">
    <div id="logo" class="col_12">And the winner is<span>n't...</span></div> 
    <div id="navigation" class="row"> 
        <ul id="pirra">
            <li><a href="#">Why?</a></li>
            <li><a href="#">Synopsis</a></li>
            <li><a href="#">Stills/Photos</a></li>
            <li><a href="#">Videos/clips</a></li>
            <li><a href="#">Quotes</a></li>
            <li><a href="#">Quiz</a></li>
        </ul>
    </div>  

it changes fine to show horizontally on CSS change

div#navigation ul li { 
    display: inline-block;
}

but now I want to do it with jQuery, I use:

$(document).ready(function() {
    console.log('hello');
    $('#navigation ul li').css('display': 'inline-block');
});

but is not working, What do i have wrong to style my element with jQuery?

thanks

Jquery Solutions


Solution 1 - Jquery

Use this:

$('#navigation ul li').css('display', 'inline-block');

Also, as others have stated, if you want to make multiple css changes at once, that's when you would add the curly braces (for object notation), and it would look something like this (if you wanted to change, say, 'background-color' and 'position' in addition to 'display'):

$('#navigation ul li').css({'display': 'inline-block', 'background-color': '#fff', 'position': 'relative'}); //The specific CSS changes after the first one, are, of course, just examples.

Solution 2 - Jquery

$('#navigation ul li').css('display', 'inline-block');

not a colon, a comma

Solution 3 - Jquery

$('#navigation ul li').css({'display' : 'inline-block'});

It seems a typo there ...syntax mistake :))

Solution 4 - Jquery

you could also specify multiple style values like this

$('#navigation ul li').css({'display': 'inline-block','background-color': '#ff0000', 'color': '#ffffff'});

Solution 5 - Jquery

I think you can use this code also: and you can manage your class css better

<style>
   .navigationClass{
		display: inline-block;
		padding: 0px 0px 0px 6px;
		background-color: whitesmoke;
		border-radius: 2px;
	}
</style>
<div id="header" class="row">
    <div id="logo" class="col_12">And the winner is<span>n't...</span></div> 
    <div id="navigation" class="row"> 
        <ul id="pirra">
            <li><a href="#">Why?</a></li>
            <li><a href="#">Synopsis</a></li>
            <li><a href="#">Stills/Photos</a></li>
            <li><a href="#">Videos/clips</a></li>
            <li><a href="#">Quotes</a></li>
            <li><a href="#">Quiz</a></li>
        </ul>
    </div>
 <script>
    $(document).ready(function() {
$('#navigation ul li').addClass('navigationClass'); //add class navigationClass to the #navigation .

});
 </script>

Solution 6 - Jquery

changing style with jquery

Try This

$('#selector_id').css('display','none');

You can also change multiple attribute in a single query

Try This

$('#replace-div').css({'padding-top': '5px' , 'margin' : '10px'});

Solution 7 - Jquery

Get the second element inside a <div> with an specific id and change the style:

var post_id = 7;
var heart = $('#' + post_id).children().eq(1);
heart.css('color','red');

Note: This may seem obvious but it troubled me and may also happen to you. Make sure the style you want to modify is of the div itself and not a style inside that div. So for example you may have something like:

<div class="loves_it"><i class="fas fa-heart" style="color:gray"></i></div>

And abviously css() method of Jquery won't work. So make sure it's as follows:

<div class="loves_it" style="color:gray"><i class="fas fa-heart"></i></div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmanuelBetancurtView Question on Stackoverflow
Solution 1 - JqueryVoidKingView Answer on Stackoverflow
Solution 2 - JqueryJakub KotrsView Answer on Stackoverflow
Solution 3 - JquerySuresh AttaView Answer on Stackoverflow
Solution 4 - JquerythemhzView Answer on Stackoverflow
Solution 5 - Jquerypedram shabaniView Answer on Stackoverflow
Solution 6 - JqueryLove KumarView Answer on Stackoverflow
Solution 7 - JqueryGassView Answer on Stackoverflow