Solved

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

Posted on 2013-11-16
2
157 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

822 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