Robert Wilkins
Robert Wilkins

Reputation: 29

How can I hide a specific span class on a page with a dynamic ID?

I'm trying to hide a class called "details" on all single event pages but not on my main events page.

The main event page is demosite.com/events/ The single event pages are demosite.com/events/?event_id=2.

I've tried with css pseudo classes but can't get it to work. I'm trying this with javascript but it's not working - as it hides the "details" class on both pages.

This is what I've tried so far.

$(function(){
  var url = document.location.href;

  if (url.toLowerCase().indexOf('http://demosite.com/events/') >= 0) {
    $('.details').hide();
  } else {
    $('.details').show();
  }
});

Upvotes: 0

Views: 70

Answers (2)

Paul Roub
Paul Roub

Reputation: 36438

All of the pages will include demosite.com/events - you're looking for pages that don't also have event_ids.

if (document.location.search.indexOf('event_id=') >= 0))
  $('.details').hide();
} else {
  $('.details').show();
}

Upvotes: 1

Avitus
Avitus

Reputation: 15968

What you have to do is see if there is a parameter being passed to the url and then hide based on that. So here is a javascript function to get the parameter and the check for it:

<script>
 $(document).ready(function() {
    var eventid = getParamterByName('event_id');
    if ( eventid != "" ) {
       $('.details').hide();
    }
    else {
      $('.details').show();
    }

});

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}


</script>

Upvotes: 1

Related Questions