$(this).find("a").attr("onclick"); onclick giving invalid character

11,122

Thats the expected behaviour.

Because, last time I checked, onclick was an event and not an attribute or property.
Also href and onclick are completely different things.

Share:
11,122
Sagar
Author by

Sagar

Love to work with CSS & Jquery

Updated on June 28, 2022

Comments

  • Sagar
    Sagar almost 2 years

    I m creating a dynamic tab using Jquery its working fine but giving error for anchor tag onclick error is invalid character. See the javascript:

    $(document).ready(function(){
    
        //when page loads...
        $(".tab_content").hide(); //hide all content
        $("ol.tabs li:first").addClass("active").show();  //Activate first tab
        $(".tab_content:first").show(); // show first tab content
    
        //on click event
        $("ol.tabs li").click(function(){
            $("ol.tabs li").removeClass("active"); //remove any "active" class
            $(this).addClass("active"); // add "active" class to selected tabs
            $(".tab_content").hide(); // hide all tab content
            //var activeTab = $(this).find("a").attr("href"); // find href attribute value to identify the active tab + content
            var activeTab = $(this).find("a").attr("onclick"); // find href attribute value to identify the active tab + content
            $(activeTab).fadeIn(); // fadeIn the active ID content
            alert(activeTab);
            return false;
        });
    
    });
    

    And the HTML:

    <div>
        <div id="tab1" class="tab_content">01</div>
        <div id="tab2" class="tab_content">02</div>
        <div id="tab3" class="tab_content">03</div>
    </div>
    <ol class="tabs">
        <li><a onclick="#tab1">Unique benefits</a></li>
        <li><a onclick="#tab2">Insurance</a></li>
        <li><a onclick="#tab3">Business Loan</a></li>
    </ol>
    

    If i use var activeTab = $(this).find("a").attr("href"); Its work fine. Help out