1

I have an website with URL`s like example.com/index.php?p=home An this script

$(document).ready(function() {

    var pathname = jQuery.url.attr("query");
    $('.tab_container').append(' .'+ pathname +' ');

    //Default Action
    $(".tab_content").hide(); //Hide all content
    $(' .'+ pathname +' ').show();

    //On Click Event
    $("#sidebar-menu a").click(function() {
        $("#sidebar-menu a").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).attr("href"); //Find the rel attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active content
        return false;
    });

});

And and html width divs that contain the pages class like

<div id="tab1" class="tab_content p=home">

Can I have an script that will remove the = sign from the path name variable and from the classes of div`s?

Thank you!

Till
  • 27,559
  • 13
  • 88
  • 122
Adrian Florescu
  • 4,454
  • 8
  • 50
  • 74
  • The short answer is that an `=` sign is not a valid CSS classname character. For more on this issue, [see this discussion](http://stackoverflow.com/questions/448981/what-characters-are-valid-in-css-class-names) – treeface Jan 04 '11 at 20:57
  • This is why I need a script that will remove the = sign! And I did that thanks to the guys below! – Adrian Florescu Jan 04 '11 at 21:20

3 Answers3

3

Because you are producing invalid HTML, it is not guaranteed how the browser will expose the classes of elements to the browser. You could try:

$('div[class*="'+pathname+'"]').attr('class',function(i,className){
  return className.replace(/=/g,'');
});

This uses the "Attribute Contains" selector to find a div with this string in it, and the attr setter method to change the value.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
2

pathname = pathname.replace(/=/g,'');
classes = document.getElementById('tab1').className;
$('#tab1').removeClass().addClass(classes.replace(/=/g,'');

Since my answer was accepted I better make it more like Phrogz's answer, which was voted up. The attr set function is nice:

$('div[class*="'+pathname+'"]').attr('class',function(i,className){  
  return className.replace(/=/g,'');  
});
pathname = pathname.replace(/=/g,'');
offex
  • 1,875
  • 5
  • 17
  • 27
0

Since, I see that the URL is a PHP page, will not it be easier to use PHP to store the variable ($_GET['p']) and then run a str_replace on it (or strtr), and pass this variable to jQuery?

(Its just a suggestion, since the page is a PHP page. I am sorry, if this doesnt apply to you)

Stoic
  • 10,536
  • 6
  • 41
  • 60
  • Thank you for fast answer, I don`t know PHP and it`s not made by me. I need something that will not need to change the PHP code. – Adrian Florescu Jan 04 '11 at 20:49