Solved

How do I create different JavaScript variables based on a single id?

Posted on 2013-11-16
2
172 Views
Last Modified: 2013-11-17
I am using the following toggle script to show and hide divs based on their id:

<script type="text/javascript">
<!--
    function toggle(id) {
       var e = document.getElementById(id);
       if(!e.style.display || e.style.display == "none"){
          e.style.display = 'block';
	   }
       else{
          e.style.display = 'none';
	   }
	}
//-->
</script>

Open in new window


The script works well, but I now want a single toggle to manipulate multiple ids.

Currently, ids are formatted as '[name]-[number]', for example "nav-3".

How can I modify this script to take the id and append extra info, such as "nav-3", "nav-3-ul", "nav-3-div" etc?

Hopefully this example helps explain what I'm after:

<script type="text/javascript">
<!--
    function toggle(id) {

       var e = document.getElementById(id); // where id is provided as "nav-3"
       var a = document.getElementById(id-ul); // using the id and appending -ul e.g. "nav-3-ul"
       var b = document.getElementById(id-div); // using the id and appending -div e.g. "nav-3-div"

       if(!e.style.display || e.style.display == "none"){
          e.style.display = 'block';
          a.style.display = 'block';
          b.style.display = 'block';
	   }
       else{
          e.style.display = 'none';
          a.style.display = 'none';
          b.style.display = 'none';
	   }
	}
//-->
</script>

Open in new window

0
Comment
Question by:93jordanaj
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 200 total points
ID: 39653970
      var e = document.getElementById(id); // where id is provided as "nav-3"
       var a = document.getElementById(id+"-ul"); // using the id and appending -ul e.g. "nav-3-ul"
       var b = document.getElementById(id+"-div"); // using the id and appending -div e.g. "nav-3-div"

Open in new window


<< + >> is used to concatenate string
Use "" or '' to create a string
0
 
LVL 1

Author Closing Comment

by:93jordanaj
ID: 39654211
Thank you very much. As you can tell JavaScript isn't be strong point and I'd been playing with concat() with no luck. This works perfectly.

Alex
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tools for analysis of code 4 44
Coding for the first time 9 62
JavaScript code not working 3 31
How to get text of href without any ID using Javascript 9 23
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question