0

I'm turning my Twitter bootstrap flashmessages in notifications for which i need to get it's type (like, success, info, error etc).

This can be done by getting a class like alert-success and substring it.

How can I get the class from my current element which start with a certain part. (in this case alert-)

Example element:

<div class="alert alert-success" role="alert">...</div>

I need to get the class and not the element

Multi-Cab
  • 11
  • 1
  • 7
  • Put the alert type in its own `data-*` attribute and read that out when needed. Hacking around a `className` string isn't a great idea – Rory McCrossan Aug 15 '16 at 14:54
  • Unless I'm missing something - http://stackoverflow.com/questions/2400386/get-class-name-using-jquery – Kobunite Aug 15 '16 at 14:59

1 Answers1

0

You could use a simple regex match on elements containing alert- in class attribute.

var cls = $('div[class*="alert-"]').attr("class").match(/alert-(.*)\s?/)[0];
eisbehr
  • 12,243
  • 7
  • 38
  • 63