1

Am working on some tabs using Jquery, if the user clicks on a specific tab, am trying to capture the text of the tab selected and do some action if the text of the tab clicked matches but it aint showing.

~ Kindly assist?

let selected = false;
$(".columns").click(function () {
    if (selected =! selected) {
        var subject = $(this).text();
        //alert(subject);
        if(subject == "Mathematics"){
            alert('Maths');
        }
        else if(subject == "English"){
            alert('Eng');
        }
        else{
            alert('Others');
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
    <li class="columns">
        <label>
            <div>
                <span>Mathematics</span>
            </div>
        </label>
    </li>

    <li class="columns">
        <label>
            <div>
                <span>English</span>
            </div>
        </label>
    </li>

    <li class="columns">
        <label>
            <div>
                <span>Swahili</span>
            </div>
        </label>
    </li>

    <li class="columns">
        <label>
            <div>
                <span>Religious</span>
            </div>
        </label>
    </li>
</ul>

5
  • 1
    Do you need to match based on tab text or could it be based on ID? What would happen if two tabs had the same name? Commented Nov 4, 2019 at 15:15
  • @Remy Want to match based on tab text,, the content is static so no tabs will have similar names.. Commented Nov 4, 2019 at 15:16
  • 1
    What do you think this line is doing? if (selected =! selected) - what do you get if you remove that if Commented Nov 4, 2019 at 15:18
  • @freedomn-m When I remove it and I alert(subject) clicked it is displayed twice,, so the selected variable captures the subject selected only when the tab is clicked once.. Commented Nov 4, 2019 at 15:23
  • Well that's an XY problem, but not the one you've asked. Maybe if you used clearer spaces it wouldn't be a question (if (selected = !selected)), but at least you know why it's there. Commented Nov 4, 2019 at 15:30

2 Answers 2

4

The problem is when you get the text it comes with spaces. use .trim() to remove them.

let selected = false;
$(".columns").click(function () {
    if (!selected) {// means false
        var subject = $(this).text();
        //alert(subject);
        if(subject.trim() == "Mathematics"){
            alert('Maths');
        }
        else if(subject.trim() == "English"){
            alert('Eng');
        }
        else{
            alert('Others');
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
    <li class="columns">
        <label>
            <div>
                <span>Mathematics</span>
            </div>
        </label>
    </li>

    <li class="columns">
        <label>
            <div>
                <span>English</span>
            </div>
        </label>
    </li>

    <li class="columns">
        <label>
            <div>
                <span>Swahili</span>
            </div>
        </label>
    </li>

    <li class="columns">
        <label>
            <div>
                <span>Religious</span>
            </div>
        </label>
    </li>
</ul>

Sign up to request clarification or add additional context in comments.

1 Comment

@Martin if you used data-value='English' then you wouldn't have to trim (and wouldn't have need to ask this question). Never use .text() for anything other than displaying text to the user.
0

If the content is static, I would recommend adding IDs, as this enforces unique naming, however, to answer your question, if you're looking to match tabs by their name, I would suggest using XPath, as you can't select by text with CSS selectors

//*[text()="Mathematics"]

If you copy the above and paste it in dev tools, it should show the highlighted matches. To use it with jQuery, use

$(document).xpathEvaluate(//*[text()="Mathematics"])

To achieve the desired outcome with ID's, which is a better practice for this use case, you're simply matching to your ID as follows:

HTML:

<span ID="Mathematics">Mathematics</span>

CSS:

#Mathematics { }

JQuery:

$("#Mathematics")

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.