WozPoz
WozPoz

Reputation: 992

JQUERY - IF the element's current value ends with !!!, trigger an Alert

Given:

var value = $this.text();

Where value equals: Phasellus pellentesque metus in nulla. Praesent euismod scelerisque diam. Morbi erat turpis, lobortis in, consequat nec, lacinia sed, enim. Curabitur nisl nisl, consectetuer ac, eleifend a, condimentum vel, sem.

When the user types in: Where value equals: Phasellus pellentesque metus in nulla. Praesent euismod scelerisque diam. Morbi erat turpis, lobortis in, consequat nec, lacinia sed, enim. Curabitur nisl nisl, consectetuer ac, eleifend a, condimentum vel, sem.!!!

The 3 !!!

I want JavaScript to Alert, allowing me to call a different function.

I'm trying to use:

if (/!!!$/.test(value)) {} but that doesn't seem to be working.

Ideas?

Upvotes: 0

Views: 558

Answers (3)

KoolKabin
KoolKabin

Reputation: 17653

$this doesnt refer to self. $(this) refers to self. so it should be like:

var value = $(this).text();

full eg.:

<body>
    <p>hello!!!</p>
    <p>done</p>
</body>
<script>
    $(document).ready( function()
    {
        $("p").click( function()
        {
            var value = $(this).text();
            if (/!!!$/.test(value)) { msg = 'done';}else{ msg = 'not done';}
            alert(msg);
        });
    });
</script>

Upvotes: 0

Amarghosh
Amarghosh

Reputation: 59451

It looks like there is a \n after the !!!. Use /m flag for making the regex multiline.

if (/!!!$/m.test(value)) {
    console.log("it works");
} 

Check this:

var s = "When the user tl, sem.The 3 !!!";

if (/!!!$/m.test(s)) 
    console.log("multiline matches");   //prints

if (/!!!$/.test(s)) 
    console.log("single line matches"); //prints

s += "\n";

if (/!!!$/m.test(s)) 
    console.log("multiline matches");   //prints

if (/!!!$/.test(s)) 
    console.log("single line matches"); //doesn't print

Upvotes: 1

Kronass
Kronass

Reputation: 5406

this code will note work your conversion to jquery object is not right, use the following

var value = $(this).text();

Upvotes: 0

Related Questions