Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:50:43+00:00 2026-06-05T18:50:43+00:00

I’m trying to add extra HTML attributes to some select options in a dropdown

  • 0

I’m trying to add extra HTML attributes to some select options in a dropdown list in Joomla 2.5, and want to use the built-in HTML helpers rather than write the HTML by myself. The current output is:

<select>
    <option value="Red">Red</option>
</select>

but I would like it to be something like:

<select>
    <option value="Red" data-img="red.jpg">Red</option>
</select>

so that I can access the data-img attribute using Javascript when the selected option changes.

I’m creating the select options like so:

$select = JHtml::_('select.option', "Red", "Red");

and then passing them to JHTML to create the generic list HTML:

$html = JHTML::_('select.genericlist', ...);

I’ve looked through the documentation and tried passing various different parameter to the functions, but it’s very confusing in terms of all the options (option.attr, attr etc) that the functions use, and Google has turned up nothing either.

Can anyone tell me what extra parameters I need to pass to the functions to get it properly add the extra attributes to the <option> elements?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:50:44+00:00Added an answer on June 5, 2026 at 6:50 pm

    I was struggling on this exact scenario today, needing to add some extra data with the select’s options. After a thorough analyze of the joomla/libraries/joomla/html/html/select.php file, I succeeded to do this with a few downside…

    First, in my case, the data used for the select is coming from the database, and need some preparation for this scenario :

    $db =& JFactory::getDBO();
    $sql = 'SELECT  nom AS value , nom AS text, prix FROM #__reservation_nourritures order by `ordering` asc';
    $db->setQuery($sql);
    $nourritures = $db->loadObjectList();
    foreach ($nourritures as $nourriture){
        //the data to use MUST be in an array form for the extra html attributes...
        $nourriture->data = array('data'=>$nourriture->prix);
    }
    

    Once the data is ready, you can pass it to the JHTML function to build the select :

    echo JHTML::_('select.genericlist',$nourriture,'myId',array('class'=>'nourritures','option.attr'=>'data'));
    

    In short, the ‘option.attr’ must be used to insert attributes into the options. Note : The select.genericlist function MUST have only 3 arguments for this to work. From what I understand from the function, attributes only get merged into the options if you pass exactly 3 arguments to the function, otherwise it just ignores it. So if you want, as exemple, to define a preselected option with the additional parameters, you are out of luck. Here is the part regarding this in the function :

    if (is_array($attribs) && func_num_args() == 3)
    {
        // Assume we have an options array
        $options = array_merge($options, $attribs);
    }
    

    To my understanding, this is a bug and/or a bad behavior. I’ll fill in a bugg in the joomla tracker when I get time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.