0

I have checked this question

But it didnt work if i want to look if that class start by special string.

im trying to achieve something like this

   <div   class = "message_holder user12121 chatid47">

and when clicking

   $(document).on ("mouseenter", ".message_holder", function () {
    this_id = $(this).find("div[class*='user']");
    alert(this_id.replace('user', '')); // i want to alert 12121
    alert(this_id); // tried this also
    })

but didnt work . im getting [object OBJECT] error.

if it was a fixed class this could work

    $(this).find(".subclass");
Community
  • 1
  • 1
Scooter Daraf
  • 525
  • 7
  • 23

1 Answers1

1

you need to add .attr("class")

$(document).on ("mouseenter", ".message_holder", function () {
   var this_id;
  
  var classList = $(this).attr("class").split(/\s+/);
  $.each(classList, function(index, item) {
    if (item.indexOf("user") > -1) {this_id = item;}
  });
   alert(this_id.replace('user', ''));
 })
.message_holder{
  height:200px;
  width:200px;
  background:silver;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="message_holder user1235 chat77"></div>

EDIT Code has been updated to take into consideration html.

EDIT 2 Used an idea from here to create an array of classes. Get class list for element with jQuery

Community
  • 1
  • 1
imvain2
  • 15,480
  • 1
  • 16
  • 21
  • please post the HTML outline, because this example works with the html that was assumed from your jquery. – imvain2 Apr 15 '16 at 20:39
  • this is my outer html `this is my outer html `
    sssss
    23 days ago
    – Scooter Daraf Apr 15 '16 at 20:41
  • ok, I see your problem. FIND actually means, FIND as CHILDREN of this object. I updated my code to fix that issue. However, the one last thing that needs to happen is an updated REPLACE. – imvain2 Apr 15 '16 at 20:51
  • Ok , isee that it returns all class names `message_holder 12121` without user . now i will remove the other names by jquery . It returns all the class . i thought it will return only that id of user – Scooter Daraf Apr 15 '16 at 20:52
  • I needed just this class of user12121 . not all class – Scooter Daraf Apr 15 '16 at 20:55
  • without creating a regular expression that might not take into consideration every variation, I updated my code to create an array of the classes and loop looking for a class with user. – imvain2 Apr 15 '16 at 21:04
  • Thanks that worked :) . i thought there is simple solution with just selecting it directly without looping , but anyway this is what i have right now . thanks so much – Scooter Daraf Apr 15 '16 at 21:07