[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2013-11-16
2
Medium Priority
?
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 800 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

650 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