JavaScript / jQuery "if" Condition Syntax

I am trying to evaluate a class to see if it contains any text in my click handler, but I cannot get my code to act correctly. What am I missing?

The operator ifsearches if the object class with the click has the word "title" in it.

$('[class^=edit_]').click(function(){
    var element = $(this).attr('class');
    var field = element.split(/_(.+)/)[1];
    if ($(this).attr('[class*=headline]'))
    {
        alert("headline");
    }
    else
    {
        alert("not headline");
    };
});

Is it possible to build an operator ifwith something that evaluates var field = element.split(/_(.+)/)[1];, since in fact the information is there.

Sort of:

if (element *= "headline"){do this};

I'm not sure I understand all the "evaluators" that exist in JavaScript to find out if I can evaluate such a line.

+3
source share
1 answer

, .className , .indexOf(), :

if (this.className.indexOf('headline') != -1)

:
, , , , , .is(), :

if ($(this).is('[class*=headline]'))

( , ), .hasClass() :

if ($(this).hasClass('headline'))
+4

Source: https://habr.com/ru/post/1785101/


All Articles