JavaScript Add css style to single element in jQuery css style single element,html style single element,wpf style

$(".className") return all the element having class .className , I want to add style only to a particular element i.e. I want to access element using their index number.

<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head> 
<body>
    <p class="para">first paragraph </p>
    <p class="para">Second paragraph </p>
    <p class="para">Third paragraph </p>
    <script>
        console.log($(".para"));
        // console.log($(".para")[0].css({"color":"red"}));
 </script>
</body>
</html>

In this code how can I add red color to the first paragraph and yellow color to second paragraph

Answer:1

When you do $(".para")[0], you get a dom element, not a jquery element. You need to convert it to jquery element again using $($(".para")[0]), then only you can change its style using jquery css method .

<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head> 
<body>
<p class="para">first paragraph </p>
<p class="para">Second paragraph </p>
<p class="para">Third paragraph </p>
<script>
   
    $($(".para")[0]).css({"color":"red"});
    $($(".para")[1]).css({"color":"yellow"});
 </script>
</body>
</html>
Answer:2

You can use jQuery .eq() to get element and update anything for this element

https://api.jquery.com/eq/

<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head> 
<body>
<p class="para">first paragraph </p>
<p class="para">Second paragraph </p>
<p class="para">Third paragraph </p>
<script>       
   $(".para").eq(0).css({"color":"red"});
    $(".para").eq(1).css({"color":"yellow"});
    
 </script>
</body>
</html>
Answer:3

You can use like this

https://codepen.io/creativedev/pen/yEVgGg

<p class="para">first paragraph </p>
<p class="para">Second paragraph </p>
<p class="para">Third paragraph </p>

$(".para:eq( 0 )").css('color', 'red');
$(".para:eq( 1 )").css('color', 'yellow');
Answer:4

If there is no event which will trigger the changes, it can be done using only css pseudo selector like first-of-type & nth-child(childIndex)

.para:first-of-type {
  color: red;
}

.para:nth-child(2) {
  color: yellow;
}
.para:nth-child(3) {
  color: green;
}
<p class="para">first paragraph </p>
<p class="para">Second paragraph </p>
<p class="para">Third paragraph </p>
Answer:5

use addClass(), parameter used is the index and class name; Check the code snippet :D

function addClass(index, classname){
  var el = jQuery('.para');
  el[index].classList.add(classname);
  console.log(el[index]);
}
addClass(1, 'active');
.active{
  color:red
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p class="para">first paragraph </p>
<p class="para">Second paragraph </p>
<p class="para">Third paragraph </p>
Answer:6

Just wanna say first off you're awesome and thank you in advance. Now onto my problem. I am making a turn based RPG game using HTML5, Javascript, and some Jquery. I'm good on mostly everything like ...

Just wanna say first off you're awesome and thank you in advance. Now onto my problem. I am making a turn based RPG game using HTML5, Javascript, and some Jquery. I'm good on mostly everything like ...

I have a Google Sheets trigger function that on form submit places submissions in sheets based on the value selected for one of my questions. I'm trying to test my function with "Test as an Add-On" ...

I have a Google Sheets trigger function that on form submit places submissions in sheets based on the value selected for one of my questions. I'm trying to test my function with "Test as an Add-On" ...

I'm having trouble trying to call a chain of asynchronous functions inside an array. When I call the function individually it works without any problem like in the example below: function ...

I'm having trouble trying to call a chain of asynchronous functions inside an array. When I call the function individually it works without any problem like in the example below: function ...

  1. calling async functions
  2. calling async functions javascript
  3. calling async function without await
  4. calling async function without await javascript
  5. calling async function without await c#
  6. calling async function c#
  7. calling async function python
  8. calling async function js
  9. calling async function nodejs
  10. calling async function in sync
  11. calling async function with await
  12. calling async function in constructor
  13. calling async function in sync c#
  14. calling async function in a loop
  15. calling async function vb.net
  16. calling asynchronous javascript functions in a sequential manner
  17. asynchronous calling method
  18. calling an asynchronous function within a for loop in javascript
  19. asynchronous calls synchronous functions

I am using this method in order to convert an Object to QueryString. QueryString is required for ajax send request. var objectToQueryString = function(a) { var prefix, s, add, name, r20, ...

I am using this method in order to convert an Object to QueryString. QueryString is required for ajax send request. var objectToQueryString = function(a) { var prefix, s, add, name, r20, ...