Site logo

[Resolved] onclick= in a menu item

Home Forums Support [Resolved] onclick= in a menu item

Home Forums Support onclick= in a menu item

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2445098
    Adrien

    Dear GP Team,

    I’ve successfully added onclick=Gleap.open(); to a button adding this code into my page as HTML block: Chat with us

    However, Im trying to add this to a menu item: onclick=”Gleap.open();
    I can’t find a way to do it.

    Would greatly appreciate your help. Thank you.

    #2445923
    Fernando
    Customer Support

    Hi Adrien,

    Which specific menu item are you planning to add it to?

    You can use WordPress filter – nav_menu_link_attributes. Reference: https://developer.wordpress.org/reference/hooks/nav_menu_link_attributes/

    Sample code:

    function add_class_to_non_top_level_menu_anchors( $atts, $item, $args, $depth ) {
    	$menu_items = array(34,395437);
    	if(in_array($item->ID, $menu_items)){
    		$atts['onClick'] = 'teGleap.open()st';
    	}
    
    	
    	return $atts;
    }
    add_filter( 'nav_menu_link_attributes', 'add_class_to_non_top_level_menu_anchors', 10, 4 );

    This code adds your onClick attribute to Menu items with IDs 34 and 395437.

    You may replace these values for your reference.

    You can find the ID of a menu item by right-clicking on it, inspecting it, the going to the Elements tab. Example: https://share.getcloudapp.com/d5uybLEA

    #2455376
    Adrien

    Thanks a lot Fernando. It worked perfectly.

    #2455437
    Fernando
    Customer Support

    You’re welcome, Adrien!

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.