1

I am trying to create a logic that if anyone enters "<p>" and "</p>" characters inside <textarea>, then only Jquery should show the win message.I have a textarea with class html, a h2 with class result which shows win or loss.By now, I have this code:

var html = $('.html').val();
if(html.indexOf("</p>" && "<p>") === -1)
{
document.getElementById("result").innerHTML = "You lost it.";
}
else{
document.getElementById("result").innerHTML = "Hurray!You won";
}

But, this code is only checking if the <p> is there and not checking for </p>.So what can I do....

3
  • 1
    html.indexOf("</p>" && "<p>") - is that valid to have && <expr> inside indexOf? Commented Sep 10, 2014 at 16:17
  • 1
    "</p>" && "<p>" === "<p>", you'll need to check both and then compare results. Commented Sep 10, 2014 at 16:17
  • @tymeJV Yes it is, as long as the result of expression(s) can be evaluated to a string. Commented Sep 10, 2014 at 16:23

3 Answers 3

5

The expression "</p>" && "<p>" is equivalent to "<p>" -- && evaluates each of its arguments from left to right, and returns the last truthy argument. Since both strings are truthy, what you wrote is effectively:

if (html.indexOf("<p>") === -1)

If you want to test whether a string contains two substrings, you have to call indexOf separately for each of them:

if (html.index("</p>") !== -1 && html.indexOf("<p>") !== -1)
Sign up to request clarification or add additional context in comments.

1 Comment

oh @Barmar.I can't get the way to thnx you more than saying it you here.Thnq very much.
2

From MDN (Logical Operators) - Logical And (&&):

Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

</p> isn't being evaluated as false, so the second value is returned which is <p>.

This means that you're only checking for the index of <p>. Try this instead:

var html = $('.html').val();
if (html.indexOf("</p>") === -1 && html.indexOf("<p>") === -1) {
    document.getElementById("result").innerHTML = "You lost it.";
}
else {
    document.getElementById("result").innerHTML = "Hurray! You won";
}

Comments

1

.indexOf takes a single string as an argument. You cannot combine string elements together using && like that.

The simplest way to modify your code would be to make two separate checks, one for the opening tag and one for the close:

if (html.indexOf("</p>") !== -1 && html.indexOf("<p>") !== -1)

This makes two separate checks for the two strings.

Alternatively, you could create a jQuery object from the HTML fragment inside your <textarea>, then check that it has no children that are <p> tags:

if ($('<div>'+html+'</div>').find('p').length > 0) {
    // the textarea contains some <p> tags
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.