0

I'm creating an Accordion Module (using Bootstrap) with the latest forum posts from Kunena.

I have written the following, which is displaying the posts correctly, but I need to somehow use jQuery to dynamically create the names for each panel:

<div class="panel panel-default">
  <div class="panel-heading" role="tab" id="headingNAME">
    <h4 class="panel-title">
      <a data-toggle="collapse" data-parent="#accordion" href="#collapseNAME" aria-expanded="true" aria-controls="collapseNAME">
            <?php echo ModuleKunenaLatest::setSubjectTitle($this, $this->message->message); ?>
      </a>
    </h4>
  </div>
  <div id="collapseNAME" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingNAME">
    <div class="panel-body">
      <?php echo KunenaHtmlParser::stripBBCode($this->message->message, $this->params->get ( 'lengthcontentcharacters' )); ?>
    <?php echo ModuleKunenaLatest::shortenLink($this->getTopicLink($this->topic, $this->message, null , ModuleKunenaLatest::setSubjectTitle($this, $this->message->message)), $this->params->get ('titlelength')); ?><?php echo JText::_('READMORE');?>
    <p class="authorPostTime">
        <span class="klatest-author"><?php echo JText::_ ( 'MOD_KUNENALATEST_POSTED_BY' ) .' '. $this->message->getAuthor()->getLink(); ?></span>
        <span class="klatest-posttime"><?php $override = $this->params->get ( 'dateformat' ); echo KunenaDate::getInstance($this->message->time)->toKunena($override ? $override : 'config_post_dateformat'); ?></span>
    </p>
    </div>
  </div>
</div>

The 'standard' Bootstrap code is:

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingOne">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Collapsible Group Item #1
        </a>
      </h4>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingTwo">
      <h4 class="panel-title">
        <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          Collapsible Group Item #2
        </a>
      </h4>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
</div>

I can use jQuery to replace NAME with 'One' (in my code), but how can I use it to replace NAME with 'One', 'Two', 'Three', etc - depending on the number of panels shown?

  • Sorry, but what are you using 'One' etc for and why can't you generate them in PHP? What does the number actually refer to? – Ville Niemi Mar 26 '15 at 11:11
  • I've updated the question. Bootstrap uses 'One', 'Two' etc to control the panels – RustyJoomla Mar 26 '15 at 11:21
  • Much clearer. Why are you not using PHP to do the numbering? Do you not know the number on server? In jQuery you could iterate thru "panel-group" children and in each children replace "NAME" with ["One","Two","Three","Four","Five"][iterationCount] (not actual syntax, obviously, but idea should be clear – Ville Niemi Mar 26 '15 at 11:42
  • There isn't a number on the server to use, hence not using PHP. Could you create an answer please so I can test it? Thanks for your help so far – RustyJoomla Mar 26 '15 at 11:46
  • @VilleNiemi could you expand on this in answer please? I can't work out your solution from your comment – RustyJoomla Mar 27 '15 at 9:01
1

I would not use jQuery to alter element ID. Best way is using PHP to do it on server before it gets to client. I do not understand You saying "not using PHP", when in your code there is "<?php echo KunenaHtmlParser::stripBBCode....."

So if the code is generated by iteration on server side, just add counter $i=1 before loop starts, and $i++ in the end of the loop. Than you can use it like this:

id="heading<?php echo $i; ?>"

Of course if You need 1 to be "One" you can look here for some solutions: https://stackoverflow.com/questions/2112571/converting-a-number-1-2-3-to-a-string-one-two-three-in-php

Good luck)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.