Get a specific class using JQuery / JavaScript

I have a div that contains some html elements. They all have a class named

class="fileItem read write"

...

But there may be an element with

class="fileItem read write selected"

...

Now I want to get this element with an additional "selected" tag and edit it. I currently have JQuery

$(".fileItem").click(function(){
    // Code
});

      

which detect a click on one of these files.

Here I want to edit the style tag of the element with the 'selected' tag. So is there something where I can say, get all over the class and choose the option with "selected"?

+3


source to share


7 replies


Just collect the class names you want, like in a CSS selector:



$(".fileItem.selected").click(function() {
    // do stuff
});'

      

+5


source


$(".fileItem").click(function(){
      $(this).find('.selected').style('blah','blah')
});

      



Something like this will allow you to perform other functions as well as the selected item.

+1


source


$(".fileItem").click(function(e) {
  e.preventDefault();
  
  $('.selected').css({
    'color': 'green'
  });
});
      

Run codeHide result


+1


source


You can just use this line of code to get an element with class selected

$(".fileItem.read.write.selected").click(function() {
    // code here
});'

      

0


source


you can use

   $(".selected").click(function(){});

      

0


source


Use the core java script for this.

function func() {
alert("Hello, world!");
}

document.getElementsByClassName("fileItem read write selected")
[0].addEventListener("click", func, false);

      

0


source


As far as I know, you should be able to combine the two class names:

$(".fileItem.selected").click(function() {
//your actions
});

      

0


source







All Articles