473,412 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

How to combine 2 different function of javascript

Hello, i'm newbie on writing HTML and Javascript code, can anyone help me to figure it out how to combine this js code
Expand|Select|Wrap|Line Numbers
  1. var data = [{dept: 'Marketing', user: []},{dept: 'IT', user: []},{dept: 'Accounting', user: []}];
  2.         function initCombo(){
  3.             var opt1 = document.getElementById('opt1');
  4.             var opt2 = document.getElementById('opt2');
  5.             for(var i=0;i<data.length;i++){
  6.                 //alert(data[i].dept);
  7.                 var option = document.createElement('option');
  8.                 option.value = i;
  9.                 option.text = data[i].dept;
  10.                 opt1.add(option);    
  11.             }
  12.             for(var i=0;i<data.length;i++){
  13.                 //alert(data[i].dept);
  14.                 var option = document.createElement('option');
  15.                 option.value = i;
  16.                 option.text = data[i].dept;
  17.                 opt2.add(option);    
  18.             }
  19.         }
  20.         function save(){
  21.             var  input = document.getElementById('input');
  22.             if (input.value != ''){
  23.                 var opt1 = document.getElementById('opt1').value;
  24.                 data[opt1].user[data[opt1].user.length] = input.value;
  25.  
  26.                 reloadOpt2();
  27.             }else{
  28.                 alert('ISI KAN DATA COI');
  29.             }
  30.             input.focus();
  31.             input.value='';
  32.         }
  33.  
  34.         function reloadOpt2(){
  35.             var opt2 = document.getElementById('opt2').value;
  36.             var optUser = document.getElementById('optUser');
  37.             var length = optUser.options.length ;
  38.  
  39.  
  40.             while(optUser.options.length != 0){
  41.                 optUser.removeChild(optUser.options[0]);
  42.             }
  43.  
  44.             for (var i=0; i<data[opt2].user.length; i++){
  45.                 var option = document.createElement('option');
  46.                 option.value = i;
  47.                 option.text = data[opt2].user[i];
  48.                 optUser.add(option);    
  49.             }
  50.         }
HTML
Expand|Select|Wrap|Line Numbers
  1. <body onload='initCombo()'>
  2.  
  3. <select id="opt1">
  4. </select>
  5. <br/>
  6.  
  7. <input type='text' id='input' value=''/>
  8. <br/>
  9.  
  10. <input type='submit' value='Simpan' onclick='save()'/>
  11. <br/>
  12.  
  13. <select id="opt2" onchange='reloadOpt2()'>
  14. </select>
  15.  
  16.  
  17.  
  18. <select id="optUser">
  19. </select>
to one of my HTML which is using javascript for changing table content

here's the js code
Expand|Select|Wrap|Line Numbers
  1. function myFunction() {
  2.     var x = document.getElementById("mySelect").value;
  3.     document.getElementById("demo1").innerHTML = "title";
  4.     document.getElementById("demo2").innerHTML = "test";
  5.     document.getElementById("demo3").innerHTML = "test";
  6.     document.getElementById("demo4").innerHTML = "test";    
  7.     }
  8.  
  9. function myFunction1(){
  10.     var x = document.getElementById("mySelect").value;
  11.     document.getElementById("demo1").innerHTML = "title";
  12.     document.getElementById("demo2").innerHTML = "using js DOM";
  13.     document.getElementById("demo3").innerHTML = "test";
  14.     document.getElementById("demo4").innerHTML = "copyright@test";
  15.     }
and HTML code
Expand|Select|Wrap|Line Numbers
  1. <button id="mySelect" onclick="myFunction1()">Home</button>
  2.     <button onclick="()">Login</button>
  3.     <button id="mySelect" onclick="myFunction()">About</button>
  4.  
  5. -----------
  6. <p align="center" id="demo1"></p></h1></center>
  7.     <p align="left" id="demo2"></p>
  8.     <p align="left" id="demo3"></p>
  9.     <p align="left" id="demo4"></p>
all I want is when i click on Login button, it will show the combo box that is in the first javascript


*sorry for my bad english*
Dec 18 '14 #1
0 1500

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Han | last post by:
I know this is possible (because preg can do almost anything!), but can't get a handle on the syntax. I have an HTML string: <font size="3"><a...
2
by: Paul M | last post by:
Hi, This is on an AS/400 which can be a little strange but I think the basic question is portable. I have a (non-C) program that needs to make series of calls to some C programs/functions....
3
by: christophe.verne | last post by:
Hi all, It's the first time I'm using groups so i hope I am posting on the good group. I''m not familiar with callback and function pointer and here is what I'd like to do: typedef struct...
5
by: Turbo | last post by:
I was trying to find whats so good about ajax. Well i cant say that... but i found a very good link on this site for learning ajax in 30 seconds:-...
8
by: Gunslinger09 | last post by:
Hi every one ! I need to create a linked list of pointers to different functions. The question is: if i have pointer lets say void (*a)(int, int) how to assigen to it addres of void...
6
by: Kiran | last post by:
Hi all, What I am trying to do is to pass a pointer to the first element of an array to a function, modify it in that function, and then print out the values of the array (which has been modified...
1
eboyjr14
by: eboyjr14 | last post by:
I need to make it so that when you're entering data into a textarea, and press shift+enter it creates a new line where the caret is at, then when you press enter it sends a message. This is like most...
19
by: viki1967 | last post by:
Hi there. I have this Function: //Funzione Calcola function calcola(frmObj, id)
1
by: Man4ish | last post by:
I am creating a graph using boost library, I am making the network using create_Network function() i am adding the vertices to this graph by creating the object g of class Graph. I am trying to...
0
by: jasper98 | last post by:
Hi all, I've got a big problem trying to combine javascript with xml. I am using the Flash Slideshow Maker: http://www.flash-slideshow-maker.com/ to make some nice photo galleries, but they've...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.