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 8037687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:59:49+00:00 2026-06-05T02:59:49+00:00

I’m very new to jquery and what would be a simple way to do

  • 0

I’m very new to jquery and what would be a simple way to do this:

My server side code spits out html like this:

<div name="destinationName" id="1">NEW DELHI</div>  
<div name="destinationName" id="2">JAIPUR</div>

<div name="destinationContent" id="1" style=";">NEW DELHI details</div>
<div name="destinationContent" id="2" style="display:none;">JAIPUR details</div>

By default New Delhi details needs to be shown. When I click on Jaipur, I’d like to display Jaipur details instead. When I click on New Delhi again, I need to only display New Delhi details. The behavior is similar to jquery tabs.

Javascript:

<script type="text/javascript">
$(document).ready(function() {
    $('div[name="destinationName"]').click(function(){
        $('div[name="destinationContent"]:visible').hide();
        $('div[name="destinationContent"][id=this.id]').show();
       });
});
</script>

JSFiddle

When I click on Jaipur, both New Delhi and Jaipur are rendered. When I debugged through the code,

$(‘div[name=”destinationContent”][id=this.id]’).show();

this.id is what is causing the problem.

How can I refer this.id in the above script ??

PS: If I replace this.id with “2”, it works fine. In the console, when I output `this’id’, it returns “2”. So, I’m not sure how to resolve this.


Also, can someone recommend me a good way to group similar divs (I’m using names to group similar divs and ids to identify an element within that group) ?

  • 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-06-05T02:59:50+00:00Added an answer on June 5, 2026 at 2:59 am

    Firstly the id is a unique identifier and by having multiple elements with the same ID can cause undesired behaviour in jQuery. Secondly, id cannot (validly) start with a number unless your page has the HTML5 doctype.

    So onto the problem you are having…

    To use the element id in the selector you need to access it outside of your selector string and concatenate the result with the rest of the selector, otherwise it’s just part of the string. The error you are seeing is correct that JavaScript doesn’t know what this.id is, so the following change should correct the error:

    $('div[name="destinationContent"][id=' + this.id + ']').show();
    

    However, this does not correct the duplicate id problem. It would be better to use a unique class attribute for each pair to link the <div>s together.

    You can also take advantage of the class attribute on the destinationContent instead of the custom name so that your jQuery selectors are easier to write (class selectors can be accessed using the (dot) .), for example in this jsFiddle

    If the details are always in the same order as the titles then you can use the index of the element to simply select the same destinationContent as the title, for example:

    Edit: Updated code with alternate solution not relying on element index.

    HTML

    <h3 class="delhi">NEW DELHI</h3>
    <h3 class="jaipur" >JAIPUR</h3>
    <hr/>
    <div class="destinationContent">
        <div class="delhi">NEW DELHI details</div>
        <div class="jaipur">JAIPUR details</div>
    </div>​​​​​​​​​​​​​​​​​​
    

    JavaScript

    $(function() {
        var contents = $('.destinationContent div');
        contents.hide();
    
        $('h3').on('click', function(){
            contents.hide();
            $('.destinationContent div.' + this.className).show();
        });
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this

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.