1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Identifying element not working

Discussion in 'JavaScript' started by Michael Barraclough, Sep 8, 2016.

  1. #1
    I want to append a class to all the <li class="jcalpro_events_event"> events so that I can use CSS to put a box around each event with a color that depends on the value of c[] (see JS code below). Any suggestions would be much appreciated.

    Javascript code
    jQuery(document).ready( function(){
      var c = document.getElementsByClassName('jcalpro_events_category'); <= WORKING
      for (i = 0; i < c.length; i++) {
        $('jcalpro_events_event').eq.addClass(c.textContent); <= NOT WORKING
        /* alert(c.textContent);*/ <=== WORKS WHEN NOT COMMENTED
       }
    });
    Code (markup):

    HTML code
    <div class="module title-on tabbeddark ">
    <h3 class="moduletitle"><span class="first_word">Future Events (JCal Pro Events)</span></h3>
    <div class="modulecontent">
    <div class="jcalpro_events tabbeddark">
    <ul class="jcalpro_events tabbeddark">
    <li class="jcalpro_events_event">
    <span class="jcalpro_events_link">
    <a href="/bookings/mab-schedule-jcal/location/11-phoenix-kenilworth-school">
    <img class="hasTooltip" title="Phoenix: Kenilworth School&lt;br&gt;1210 N 5th Ave" src="/media/jcalpro/images/events/icon-event-location.png" />
    </a>
    <a href="/bookings/mab-schedule-jcal/99-contra/149-phoenix-2nd-saturday" title="Phoenix 2nd Saturday Contra">Phoenix 2nd Saturday Contra</a>
    </span>
    <span class="jcalpro_events_date">Sat. Sep 10, 2016 7:00 pm</span>
    <span class="jcalpro_events_category"><a href="/bookings/mab-schedule-jcal/99-contra">Contra</a></span>
    <span class="jcalpro_events_description">Band: Clusterfolk</span>
    </li>
    
    ... more <li>s
    </ul>
    </div>
    HTML:
     
    Last edited by a moderator: Sep 8, 2016
    Michael Barraclough, Sep 8, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    You're mixing regular javascript with jQuery (why?), and you're missing the class definition inside the for loop (missing . before jcalpro_events_event)
     
    PoPSiCLe, Sep 8, 2016 IP
  3. Michael Barraclough

    Michael Barraclough Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thanks. I have now solved my problem. The final code is:

    jQuery(document).ready( function(){
    var l = document.getElementsByClassName("jcalpro_events_event");
    var c = document.getElementsByClassName('jcalpro_events_category');
    for (i = 0; i < c.length; i++) {
    l.classList += ' ' + c.textContent;
    } );
     
    Michael Barraclough, Sep 8, 2016 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    And... again... why are you mixing jQuery into that? If you're gonna load jQuery, it makes sense to actually use jQuery.
    As I understand it, you're wanting to change the jcalpro_events_event class to something else (add another class) when the jcalpro_events_category is a specific value? Or contains a specific value, or something?

    You should be able to do this without much else than a couple lines of jQuery - could you give an example of the value in jcalpro_events_category which should trigger a change? And maybe also provide what the change should look like?

    Btw, the code you posted doesn't work - among other things, you're missing a ending bracket.
     
    PoPSiCLe, Sep 9, 2016 IP
  5. Michael Barraclough

    Michael Barraclough Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Thank you. There is no trigger event. The Joomla module produces the code and I want to add a color-coded border around each <li> element in the module output with the color depending on the text of the category <span> for that <li>. (There are no IDs in the module output.)

    As I see it, I have to have a list of the relevant <li> elements and a list of the category <span> texts. I then have to assign the extra class to each of the <li> elements so that the CSS can pick this up and render the border color accordingly. I am using jQuery because it is already loaded and because I could get it to work. I would be happy to use anything that worked!
     
    Michael Barraclough, Sep 9, 2016 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Then go through each jcalpro_events_category, match the text and assign css to the parent li element?
     
    $('.jcalpro_events_category').each(function() {
      if ($(this).text() == 'whatever') {
        $(this).parents('li').css({'border':'1px solid red'});
      } 
    }) 
    
    Code (markup):
     
    PoPSiCLe, Sep 11, 2016 IP