improve js

This code updates the data in two list boxes per arrays included:


$(document).ready(function(){

var groupNames = ["Group 1x", "Group 2x", "Group 3x", "Group 4x"];
var subgroupNames = ["Sub G  1x", "Sub G 2x", "Sub G 3x", "Sub G 4x"];

alert('JQuery Test');

$(groupNames).each(function(index, item) {
    $("#group option").eq(index).text(item);
});

$(subgroupNames).each(function(index, item) {
    $("#subgroup option").eq(index).text(item);
});

$("#group").click(function() {
    alert("You have clicked on group control");
});
$("#subgroup").click(function() {
    alert("You have clicked on sub group control");
});

});

If there is no data in the list boxes when the html is launced, it doesn update.

Question: Can you revise this code to use groupNames.lenght, for example, to cycle through the code to update the contend of the list box?

The existing code is using the list item number, groupNames.eq(index).text(item);


Thank you
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
 
Tom BeckConnect With a Mentor Commented:
I would not post something like that without testing. You should not assume the problem is on my end.

Here is my complete test page. Tested successfully in Firefox, chrome and Internet Explorer.
<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<script type="text/JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

	<script type="text/javascript">			

		$(document).ready(function(){

var groupNames = ["Group 1x", "Group 2x", "Group 3x", "Group 4x"];
var subgroupNames = ["Sub G  1x", "Sub G 2x", "Sub G 3x", "Sub G 4x"];

alert('JQuery Test');

$(groupNames).each(function(index, item) {
    var $option = $('<option>');
    $option.append(groupNames[index]);
    $("#group").append($option);
});

$(subgroupNames).each(function(index, item) {
    var $option = $('<option>');
    $option.append(subgroupNames[index]);
    $("#subgroup").append($option);
});

$("#group").click(function() {
    alert("You have clicked on group control");
});
$("#subgroup").click(function() {
    alert("You have clicked on sub group control");
});

});
	</script>
<style>
</style>

	</head>
<body>
	<div id="container">

		<div id="top">

			<h1>User Input</h1>

		</div> <!-- end top div -->



    <div id="leftnav">

        <div id="navcontainer">

		<!--

          <ul>

            <li><a href="index.htm">home</a></li>

            <li><a href="product.htm">product</a></li>

            <li><a href="designer.htm">designer</a></li>

            <li class="current"><a href="aboutus.htm">about us</a></li>

          </ul> -->

            <label for="group">Group:</label>

            <select name="group" id="group" size="6" multiple="multiple">

            </select>

            <br /> <br />

            

			<label for="subgroup">Sub Group:</label>

            <select name="subgroup" id="subgroup" size="6" multiple="multiple">

            </select>   

			

      	</div> <!-- end navcontainer div -->

    </div> <!-- end leftnav div -->

	

	<!-- <h1 id="group">click response on group</h1>

	<script src="groupdata.js"></script> -->

	

	<div id="content">

	<h1>Info</h1>

		Phone: 222-2222<br/>

		Email: <a href="mailto:xy">Send email to us</a>



	<br /><br /><br />	

	<!--<form id="contact_form" action="process_form.htm" method="get"> 

	    process_form.htm is not added because I am emailing the data-->

 

  <form id="contact_form" action="mailto:xy" method="post"> 

  	<table>

  		<tr>

  			<td class="formlabel"><label for="fullname">Name:</label></td>

  			<td><input type="text" name="Name" id="fullname" /></td>

  		</tr>

  		<tr>

  			<td><label for="email">Email Address:</label></td>

  			<td><input type="text" name="Email Address" id="email" /></td>

  		</tr>

  		<tr>

  			<td><label for="address">Address:</label></td>

  			<td><textarea cols="20" rows="4" name="Address" id="address"></textarea></td>

  		</tr>

  		<tr>

  			<td><label for="comments">Order Description...</label></td>

  			<td><textarea cols="20" rows="4" name="Comments" id="comments"></textarea></td>

  		</tr>

  		<tr>

  			<td><label for="clothingtype">Type:</label></td>

  			<td>

  				<select name="Clothing Type" id="clothingtype">

  					<option value="T-Shirt" selected="selected">A</option>

  					<option value="Shirt">B</option>

  					<option value="Jacket">C</option>

  					<option value="other">D</option>

  				</select>

  			</td>

  		</tr>

  		<tr>

  			<td>XYZ:</td>

  			<td>

  				<input type="radio" name="shipping" id="reg" value="Regular Mail" checked="checked" />

  				<label for="reg">AA</label><br />

  				

  				<input type="radio" name="shipping" id="overnight" value="Over Night" checked="checked" />

  				<label for="overnight">BB</label><br />			

  			</td>

  		</tr>

  

  		<tr>

  			<td colspan="2" id="formbuttons">

  				<input type="reset" value="Clear form" /> 

  				<input type="submit" value="Order" />

					<p>For more information click 

					  <a href="#" target="_blank">here.</a>

					</p>

  			</td> 

  		</tr>

  	</table>

	</form>

	

	</div>  <!--end content div -->



  <div id="footer">

  	INFO &copy; 2012

  </div> <!-- end footer div -->

		

  </div> <!-- end container div -->
</body>
</html>

Open in new window

0
 
Tom BeckCommented:
If there is no data in the list boxes when the html is launced, it doesn update.

True, but if you have <option>s in place, it will fill them, replacing the original text. Is this not what you want? If not, then you will need to start with an empty <select> and append the <option> tags to it.

You don't gain anything using groupNames.length because jquery already knows the length. by calling .each(), jquery will loop through each item in the container.
The existing code is using the list item number
Not really. index is a zero based index of the items in the array, not the list item numbers. item is the current option item for each iteration of the loop.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
In the revised html below, I have entered one item into the group list box. Although the array groupNames  has 4 items in it, when it runs the list box is showing only one item (Group 1x).

So, this is not good. The array lenght is going to change from time to time.

Question: How the code below or js submitted earllier should change to all of the items in the arrays append to the list boxes. Please revise the code instead of describing it.

Thank you.

var groupNames = ["Group 1x", "Group 2x", "Group 3x", "Group 4x"];


    <div id="leftnav">
        <div id="navcontainer">
            <label for="group">Group:</label>
            <select name="group" id="group" size="6" multiple="multiple">
                <option value="1">Group A</option>
             <!--   <option value="2">Group B</option>
                <option value="3">Group C</option>
                <option value="4">Group D</option>      -->            
            </select>
            <br /> <br />
           
                  <label for="subgroup">Sub Group:</label>
            <select name="subgroup" id="subgroup" size="6" multiple="multiple">
                <option value="1">Sub Gro A</option>
                <option value="2">Sub Gro B</option>
                <option value="3">Sub Gro C</option>
                <option value="4">Sub Gro D</option>
            </select>  
                  
            </div> <!-- end navcontainer div -->
    </div> <!-- end leftnav div -->
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Tom BeckCommented:
Here's my <select>s
<label for="group">Group:</label>

            <select name="group" id="group" size="6" multiple="multiple">

            </select>

            <br /> <br />

            

			<label for="subgroup">Sub Group:</label>

            <select name="subgroup" id="subgroup" size="6" multiple="multiple">

            </select>

Open in new window

If you want to start with no options in the <select> then they must be added by the jquery. This  $("#group option").eq(index).text(item); does not work as a selector because there are no options so eq(index) is nothing.
<script type="text/javascript">			

		$(document).ready(function(){

var groupNames = ["Group 1x", "Group 2x", "Group 3x", "Group 4x"];
var subgroupNames = ["Sub G  1x", "Sub G 2x", "Sub G 3x", "Sub G 4x"];

alert('JQuery Test');

$(groupNames).each(function(index, item) {
    var $option = $('<option>');
    $option.append(groupNames[index]);
    $("#group").append($option);
});

$(subgroupNames).each(function(index, item) {
    var $option = $('<option>');
    $option.append(subgroupNames[index]);
    $("#subgroup").append($option);
});

$("#group").click(function() {
    alert("You have clicked on group control");
});
$("#subgroup").click(function() {
    alert("You have clicked on sub group control");
});

});
	</script>

Open in new window

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
tommyBoy,

I guess you didn't got the chance to test it because it doesn't work. I even added

$(document).ready(function(){
alert("here");
.
.

});
but nothing happening (no items show in the list boxes and there is no response for clcik action on the list boxes although add added a few items to the list boxes to check click event).

--------------

$(document).ready(function(){
<script type="text/javascript">                  
alert("here");
            $(document).ready(function(){

var groupNames = ["Group 1x", "Group 2x", "Group 3x", "Group 4x"];
var subgroupNames = ["Sub G  1x", "Sub G 2x", "Sub G 3x", "Sub G 4x"];

alert('JQuery Test');

$(groupNames).each(function(index, item) {
    var $option = $('<option>');
    $option.append(groupNames[index]);
    $("#group").append($option);
});

$(subgroupNames).each(function(index, item) {
    var $option = $('<option>');
    $option.append(subgroupNames[index]);
    $("#subgroup").append($option);
});

$("#group").click(function() {
    alert("You have clicked on group control");
});
$("#subgroup").click(function() {
    alert("You have clicked on sub group control");
});

});
      </script>
});
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you very much for the solution.

Mike
0
All Courses

From novice to tech pro — start learning today.