4

I have a page where a specific div shows the ID for the user's "system role" like so:

<div id="systemRoleIndicator" style="display: none;">
    <p>Z</p>
</div>

I can detect the value fine with jQuery, but I can't seem to actually use it for anything. For instance, I never receive the completion message in this code:

<script src="/scripts/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var role = $("#systemRoleIndicator").text();
    alert("Your role is: " + role); //displays proper value
    if (role == "Z") {
        alert("Code completed!");
    }
</script>

Why might this be? Am I missing something about comparing strings?

1 Answer 1

6

Text for #systemRoleIndicator will also return leading and trailing space near p node. which returns text " Z " and not "Z".You need to either compare with the text of p element.:

var role = $("#systemRoleIndicator p").text();

or trim the text content of #systemRoleIndicator:

var role = $("#systemRoleIndicator").text().trim();
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, that makes perfect sense. Thanks!

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.