0

What this should do: It adds a Class to the first Child. The second one is just for testing. It should change the background-color of one list-element to red. None of those applies.

clientStuff();
alert("Hi");

function clientStuff(){
  $('.client-unit').first().addClass('active-client');
}

$( "li" ).first().css( "background-color", "red" );

What i've tried:

  1. I checked the linking of the documents. And their order. Here comes the linking:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="styles.css"  rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<script src="script.js"></script>
  1. I checked the semicolons and for spelling mistakes.
  2. I asked mighty Cthulu for help. He said "no."

Does anyone have an idea?

7
  • 3
    is it inside a $(document).ready() ? Commented Jul 21, 2017 at 11:49
  • 6
    Have you asked Mighty DevConsole? Commented Jul 21, 2017 at 11:50
  • 3
    can you post your html too, please? Commented Jul 21, 2017 at 11:51
  • 1
    ...And have you defined $. Also, mighty Cthulu requires a sacrificial offering from you first before consenting to offer any assistance. Commented Jul 21, 2017 at 11:53
  • Please post HTML Commented Jul 21, 2017 at 11:54

5 Answers 5

1

Try something like:

$(function(){

  var $li = $('ul li').click(function() {
    $li.removeClass('selected');
    $(this).addClass('selected');
  });

});
li.selected {
    color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
  <li>Item 6</li>
</ul>   

Try this, If you need to select only first child:

$('ul li:first')
Sign up to request clarification or add additional context in comments.

Comments

1

Check this, I Hope its helpful to you

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
     <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
     <link href="styles.css"  rel="stylesheet" type="text/css">
     <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
     <script src="script.js" type="text/javascript"></script>

In script.js file like that below :

$(document).ready(function() {
     alert("Hi");
     clientStuff();
     $( "li" ).first().css( "background-color", "red" );

});
function clientStuff(){
    $('.client-unit').first().addClass('active-client');
}

1 Comment

I'd like to give you a brofirst and a cookie. It works. Thank you very much :)
0

$( "li" ).first().css({"background-color": "yellow"});

Comments

0

Since You are using Jquery.

so use like that code it will work.

    jQuery(function(){
      $('.client-unit').first().addClass('active-client');
      $('li').first().css("background-color","red");
    });

Comments

0

Actually first() attribute does not apply to child attributes rather it applies to first of it's own kind,

Therefore the first() searches for first element which has class client-unit and applies class to that,

Here is the sample screenshot of the output : screenshot

Also here is the Codepen of your code, with sample HTML from my side,

https://codepen.io/shohil06/pen/ZJzEJj

Hope it helps !

Thank You

1 Comment

Please edit the externally hosted code into the post; doing so will make sure it remains useful even if the link breaks. My script is not allowed to do this because of potential licensing problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.