2

I need to use a custom.js override file to add a new link to an unordered list on three pages. I must use jquery, per the systems requirements, and finding a way to select and insert is really numbing my brain.

Here is the menu block itself:

<div class="nav-block nav-block-logo">
  <div class="nav-block-body">
    <div class="nav-block-content">
      <div class="logo">
    <a href="/"><img src="logo.png"></a>
      </div>
      <h2>Contact Us</h2>
      <ul>
      <li class="active">
      <a href="/contact-us/">Overview</a>
      </li>
      <li>
      <a href="/contact-us/contact-us">Contact Us</a>
      </li>
      <li class="last-child">
      <a href="/contact-us/facilities-and-directions">Facilities &amp; Directions</a>
      </li>
      </ul>
      <a href="http://www.addthis.com/bookmark.php" class="bookmark-share addthis_button">Bookmark &amp; Share</a>
    </div>
  </div>
</div>

I need to add a new li link at the end. I've tried after() and append(), and neither have worked. I was told I may need to target the h2 and check to see if the title is "contact us" or not. Not sure how to do this either.

I am relatively new to jquery, and this was kinda dumped on me at last minute. Any help would be greatly appreciated!

3 Answers 3

2

I used class targeting. It's pretty reliable compared to looking for content, and certainly faster. I also made sure to update your last-child classes.

var $list = $(".nav-block .nav-block-content ul:first");
$list.find(".last-child").removeClass("last-child"); 
var $li = $("<li>", {"class": "last-child"}).html('<a href="#">Our Blog</a>');
$list.append($li);

With regards to the selector, I put in the easiest one but you can be more specific if necessary. For example:

var $list = $(".nav-block .nav-block-content ul:first")
Sign up to request clarification or add additional context in comments.

3 Comments

Looks solid. I tried it. Didn't work. :( I have no idea why I can't get something so simple to work.
Typo on line 3, I fixed it, give it another shot. I hate handling strings in JS.
var $list = $(".nav-block .nav-block-content ul:first") $list.find(".last-child").removeClass("last-child"); var $li = $("<li>", {"class": "last-child"}).html('<a href="">Our Blog</a>'); $list.append($li); Worked! Thanks!
2

You can use contains selector:

var $li = $('<li>', {
  'id': 'id',
  'class': 'class'
}).html('<a href="herf">text</a>');

​$('h2:contains("Contact Us")').siblings('ul').append($li)​​​;    ​
// $('.nav-block-logo ul)').append($li)​​​;

http://jsfiddle.net/fj2K5/

3 Comments

I don't think you should use his proposed solution. Why not just target the list based on class? Content is subject to change.
I have, there's nothing there saying you can't use selector targeting
hmm, interesting looks fine +1 bruver, unless I missed anything. :)
1

Assuming your new li element is a DOM object:

$('div.nav-block-content ul').append(newLI);

will add it as the last child of the ul.

Here's a fiddle (which also demos how to move the last-child class to the correct element): http://jsfiddle.net/EwMSB/

1 Comment

It works in the fiddle I set up. Are you wrapping it in a $(document).ready() function?

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.