Erdnuss
Erdnuss

Reputation: 317

Get Specific class with Jquery/ JavaScript

I have got a div containing some html elements. They all have a class named

class="fileItem read write".

But there could be an Element with

class="fileItem read write selected".

Now I want to get this element with the additional 'selected' tag and edit this. Currently I have an in JQuery

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

which detect an click on one of these fileItems.

Here I want to edit style tag of the element with 'selected' tag. So is there something where I can say get all by class and select the on with "selected"?

Upvotes: 3

Views: 127

Answers (7)

Calaris
Calaris

Reputation: 158

As far as I know you should be able to combine the two classnames:

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

Upvotes: 0

PotatoManager
PotatoManager

Reputation: 1775

Use core java script to do this.

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

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

Upvotes: 0

Nick De Jaeger
Nick De Jaeger

Reputation: 1249

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

Upvotes: 1

Aslam
Aslam

Reputation: 9690

You can use

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

Upvotes: 0

Squiggs.
Squiggs.

Reputation: 4474

$(".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.

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 30729

You can simply use this line of code to get the element with the selected class

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

Upvotes: 0

Lars Beck
Lars Beck

Reputation: 3584

Just chain the desired class names like you would do in a CSS selector:

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

Upvotes: 5

Related Questions