Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6023767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:03:16+00:00 2026-05-23T04:03:16+00:00

This is a fragment of an html doc my app has generated from a

  • 0

This is a fragment of an html doc my app has generated from a webserver log file:

<div class='ip' id='220.181.108.95'>
  <h3>220.181.108.95</h3>
  <h4 class='records' id='220.181.108.95'>
    Records:
  </h4>
  <div class='records' id='220.181.108.95'>
    <div class='record' id='220.181.108.95'>
      <p>ip: 220.181.108.95 timestamp: 15/Jun/2011:21:21:14 +0100 access_request_type: GET page: / status_code: 200 </p>
    </div>

    <div class='record' id='220.181.108.95'>
      <p>ip: 220.181.108.95 timestamp: 13/Jun/2011:08:18:09 +0100 access_request_type: GET page: / status_code: 200 </p>
    </div>

    <div class='record' id='220.181.108.95'>
      <p>ip: 220.181.108.95 timestamp: 12/Jun/2011:13:23:11 +0100 access_request_type: GET page: / status_code: 200 </p>
    </div>
  </div>
</div>

<div class='ip' id='123.125.71.60'>
  <h3>123.125.71.60</h3>
  <h4 class='records' id='123.125.71.60'>
    Records:
  </h4>
  <div class='records' id='123.125.71.60'>
    <div class='record' id='123.125.71.60'>
      <p>ip: 123.125.71.60 timestamp: 11/Jun/2011:04:19:31 +0100 access_request_type: GET page: /stylesheet.css status_code: 200 </p>
    </div>  
  </div>
</div>

Here is some JQuery I’ve been using to toggle the records div:

<script>
      $(document).ready(function(){
            $("h4.records").click(function(){
                  var id = $(this).attr("id");
                  $("div#".concat(id, ".records")).toggle();
            });
      });
</script>

I’ve two problems with the way it’s functioning:

  1. It only works when the id is a letter or string of letters. Numbers and dots stop the toggle working. Replace the id’s with “a” and “b” respectively and it toggles.
  2. It may toggle, but then it toggles all divs with a class of “record:, even though I’ve passed in elem#id.class as the selector to toggle.

Is there a way I can keep the id as the ip-address (which is the obvious unique reference for the record) and toggle only the exact div by class and id and not all divs with that class?

Any help is much appreciated. I know several languages but it’s rare I write in javascript, and I’ve not used JQuery before.

JQuery version is 1.6.1, JQueryUI is also being loaded, version 1.8.13.


Some quick notes:

  1. Pretty much everyone had a hand in getting me to the code I’m using now, so it’s unfortunate I can only pick one correct answer.
  2. @Liangliang Zheng’s answer works, but didn’t work once I added in a “hide on load” javascript function, so @Matt Ball gets the tick for a more flexible solution.
  3. I’ve started to use a custom attribute for the ip address, thanks to @Dhruva Sagar and @Liangliang Zheng for suggesting it.
  4. Ip addresses elsewhere are now prefixed, thanks to @James Khoury for that suggestion.
  5. I’ve no idea why anyone would get downvoted for their answers, they’re all at least partially correct.

I’ve put the solution to work in an app for reading webserver access logs and checking their geographical info via the ip, the code is up on Github if anyone’s interested https://github.com/yb66/GeoIPalise. I’ll give everyone here props with the next git push! Thanks to you all!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:03:17+00:00Added an answer on May 23, 2026 at 4:03 am

    The problem is twofold:

    1. As I commented on the question, you’re trying to select an element by ID when that ID contains characters which have special meaning. You need to escape those characters, or use document.getElementByID.
    2. As @Dhruva points out, the DOM must not contain elements with duplicate IDs. Use a class instead, or strip out the duplicate IDs entirely. Once the DOM contains more than one element with a given ID, all bets are off, and Cthulhu will probably be summoned shortly thereafter.

    All that said, you can just use .closest() and forget about element IDs entirely.

    HTML

    <div class='ip' id='220.181.108.95'>
      <h3>220.181.108.95</h3>
      <h4 class='records'>
        Records:
      </h4>
      <div class='records'>
        <div class='record'>
          <p>ip: 220.181.108.95 timestamp: 15/Jun/2011:21:21:14 +0100 access_request_type: GET page: / status_code: 200 </p>
        </div>
    
        <div class='record'>
          <p>ip: 220.181.108.95 timestamp: 13/Jun/2011:08:18:09 +0100 access_request_type: GET page: / status_code: 200 </p>
        </div>
    
        <div class='record'>
          <p>ip: 220.181.108.95 timestamp: 12/Jun/2011:13:23:11 +0100 access_request_type: GET page: / status_code: 200 </p>
        </div>
      </div>
    </div>
    
    <div class='ip' id='123.125.71.60'>
      <h3>123.125.71.60</h3>
      <h4 class='records'>
        Records:
      </h4>
      <div class='records'>
        <div class='record'>
          <p>ip: 123.125.71.60 timestamp: 11/Jun/2011:04:19:31 +0100 access_request_type: GET page: /stylesheet.css status_code: 200 </p>
        </div>  
      </div>
    </div>
    

    JavaScript

    $(function(){
        $("h4.records").live('click', function(){
            $(this).closest('div.ip').find('div.records').toggle();
        });
    });
    

    Other WTFs:

    • Use this.id instead of $(this).attr('id'). (See When to use Vanilla JavaScript vs. jQuery?)
    • Specifying an element type along with an ID is completely unnecessary since, as mentioned, IDs must be unique.
    • .concat()? Really? What’s wrong with + (the concatenation operator)?

    (No personal offense meant. It’s the code, not you.)

    Demo: http://jsfiddle.net/mattball/fE9Xf/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a webpage containing this fragment of HTML: <div class=a b></div><div class=a></div> .
I want to override the onAttach() http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity ) method of a ListFragment from the
Given the following HTML fragment: <div class=word> <input type=text name=A /> <input type=text name=n
I'm rendering the following html fragment... <div class=navbar navbar-fixed-top> <div class=navbar-inner> <div class=container> <%=
so this is a fragment of a procedure that exports a dataset from access
This is an example xml from MSDN <?xml version=1.0?> <!-- A fragment of a
Given the following HTML fragment: <span name=foo class=foo-class> <input name=foo value=0 id=foo2_0 type=radio> <label
I have this HTML code fragment (in a valid document using strict doctype): <p>Without
It's easy to reproduce this problem using a simple html fragment (save as test.html):
I have this fragment that demonstrates the problem: <html> <head> <title>height query demo</title> <script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.