Solved

improve js

Posted on 2012-04-11
6
196 Views
Last Modified: 2012-04-12
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
0
Comment
Question by:Mike Eghtebas
  • 3
  • 3
6 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37835302
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
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 37835396
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
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37835437
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 37837871
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
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 37838374
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
 
LVL 33

Author Closing Comment

by:Mike Eghtebas
ID: 37838595
Thank you very much for the solution.

Mike
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now