?
Solved

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

Posted on 2013-11-16
2
Medium Priority
?
180 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

741 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