WordPress.org

Ready to get started?Download WordPress

Forums

Fetch Widget names for Javascript (1 post)

  1. o3fingers
    Member
    Posted 6 years ago #

    I've got a bit of javascript to expand and collapse list items by id. I'm going into functions.php, and adding some code to the register_sidebar array to achieve this... Adding the link to call the javascript function is no problem, except the variable that should produce the widget id just outputs the string "%1$s".

    I need to call the function and pass it the name of the list item. The "%i$s" in 'before_widget' is converted into the widget name properly, the the %1$s in 'before_title' is ignored.

    Is the %1$s handled by the code for the widget, or is it coming from WordPress itself? I don't recognize the syntax, but I'm pretty new to this. My theme works fine, but I want to make it compatible with widgetized sidebars without losing any functionality.

    php code from functions.php

    if ( function_exists('register_sidebar') )
        register_sidebar(array(
            'before_widget' => '<li id="%1$s" class="widget %2$s"><ul>',
            'after_widget' => '</ul></li>',
            'before_title'=>'<a href="javascript:switchit(\'%1$s\')"><h2 class="widgettitle">',
            'after_title' => '</h2></a>',
        ));

    javscript

    function switchit(list){
    
       var listElementStyle=document.getElementById(list).style;
       if (listElementStyle.display=="none")
          listElementStyle.display="block";
       else
          listElementStyle.display="none";
    }

    Any help, or just a nudge in the right direction, is greatly appreciated!

Topic Closed

This topic has been closed to new replies.

About this Topic