0

I am trying to get the value of

  • tag

    <?php
    $i = 0;
    foreach ($categories as $key => $category) {
        ?>
        <div class="col-6 col-md-4 col-lg-2 text-center category">
            <li class="main-li" id="<?php if ($i == 0) {
                $i = 1;
                echo 'active-nav';
            } ?>" value="<?php echo $key ?>">
                <?php echo $category ?>
            </li>
        </div>
    <?php } ?>
    

    This is my jquery

    $(".category").click(() => {
        var category_id = $('li #active-nav').attr('value');
        alert(category_id);
    });
    

    Anybody can help be get the value when i click each category div

  • 1
    • Id of an element should be unique, So please try to get value based on this LIke var category_id = $(this).attr('value'); Commented Jul 21, 2020 at 4:45

    2 Answers 2

    1

    Your selector is wrong. It should be:

    "li#active-nav"
    

    ...without the space. "li #active-nav" is searching for an element with id="active-nav" within a list item

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

    Comments

    0

    I was using the wrong selector..Should have done this.

    var category_id = $('.category #active-nav').attr('value');
    

    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.