0

need some help building the logic syntax for this JQUERY condition.

if the current url window includes a specific $string (index.php) then i want to add a value to a specific <li> class. as seen below.

<ul class="nav">
  <li>...</li>
  <li>index</li>
</ul>

this is the target element ('.nav li:nth-child(2) ').addClass("selected").

this is my jquery. im not quite sure how to code the correct syntax for my logic.

 $j(function(){
    var url = window.location.pathname;
    var string = 'index.php';

    if //current url has $string ('index.php') {
     $j( ".nav li:nth-child(2)" ).addClass( "active" );
    });
 });
1

4 Answers 4

1

Have you tried?

var url = window.location.pathname;
var string = 'index.php';
if(url.indexOf(string) !== -1) {
   $j( ".nav li:nth-child(2)" ).addClass( "active" );
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you. but would you mind explain to me this part (url.indexOf(string) !== -1) why !== -1?
@user1933824 Sure. String.indexOf returns the index of the first occurrence of the specified value or -1 if the value is not found. So > -1 and >= 0 would be also correct to check if the url has some string value
0

You can use the following: http://api.jquery.com/contains-selector/

This allows you to search for a specific string (case sensitive)

1 Comment

This is not what the op is looking for, though. This could work on [<element> {search_term} </element>].contains({search_term}); NOTE: This is not valid code, also the [] notation is to indicate a jQuery object. He needs to search the url for the string, not inside an element (or a collection of them).
0

You could also use.

var url = window.location.pathname;
var your_string = 'index.php';

if(url.contains(your_string) === true ) {
    $j( ".nav li:nth-child(2)" ).addClass( "active" );
}

or

if(url.indexOf(your_string) !== -1) {
    $j( ".nav li:nth-child(2)" ).addClass( "active" );
}

Comments

0

This would be better done on the server-side, since the window.location.path won't contain index.php in redirect and default page scenarios.

Just echo a <script> tag or a global setting variable from index.php:

<!-- in index.php -->
<script type="text/javascript">
     Settings.isIndex = true;
</script>

And in script.js:

var Settings = { isIndex: false };

$j(function(){
    var url = window.location.pathname;
    var string = 'index.php';

    if (Settings.isIndex) {
      $j( ".nav li:nth-child(2)" ).addClass( "active" );
    });
});

1 Comment

Maybe index.php was just an example. What if he can't control the server-side? Also, wouldn't it be easier to simpler to just echo the additional class into the element's class attribute directly from the server-side, then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.