Home Ask Login Register

Developers Planet

Your answer is one click away!

Raul February 2016

Replace ID to an element for a particular url

I have two list items in navigation:

<body id="trips">
      <li><a href="/trips.php" class="trips">Trips</a></li>
      <li><a href="/trips.php?offer=true" class="offers">Offers</a></li>

I need to replace on <body id="trips"> with <body id="offers"> when visiting /trip.php?offer=true because I am using CSS to make current page:

#trips.trips, #offers.offers { css for "current page" button }

Any idea? :)


Adam Azad February 2016

Try this, you need to add this in trips.php

   $bodyID = 'trips'; // default value
   // when `offer` GET is set
   if(isset($_GET['offer']) && trim($_GET['offer']) == 'true'){
      $bodyID = 'offers';
<body id="<?php print $bodyID; ?>">

Viktor Kukurba February 2016

You also can add script inside body tag, which will set needed id.

  document.body.id = location.href.split('?')[1].contains('offer=true');

SHBelsky February 2016

If you're already using PHP, you could determine if the GET variable for offer is set to true, and set an id variable accordingly:

if(isset($_GET['offer'])) { # If the GET variable is set
    if($_GET['offer'] == "true") { # If the GET variable is equal to true
        $id = "offers"; # Set our ID equal to offers
else { # If the GET variable is not set
    $id = "trips"; # Set our ID equal to trips

You'd then have HTML with the following:

<body id="<?php if(isset($id)) { print $id; } ?>">

Post Status

Asked in February 2016
Viewed 3,161 times
Voted 9
Answered 3 times


Leave an answer

Quote of the day: live life