Solved

js html hookup...

Posted on 2012-04-09
4
256 Views
Last Modified: 2012-04-10
I have:

<html>
  <head>
        <title>Polulate List Box</title>
  </head>

  <body>
      <h1>Polulate List Box</h1>
      
            <select name="drop1" id="Select1" size="6" multiple="multiple">
            <option value="1">Group 1</option>
            <option value="2">Group 2</option>
            <option value="3">Group 3</option>
            <option value="4">Group 4</option>

</select>

  </body>
</html>

Question 1: How can I hook this code to groupData.js file below to dynamically to pull the data from js file.

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

var select = $("<select/>");
$(groupNames).each(function() {
    select.append($("<option/>").text(this.toString()));
});

$("h1").after(select);

Question 2: How can I add event handler so upon click on an item (Group 2), something like

You clicked on Group 2

to popup.

Thank you.
0
Comment
Question by:Mike Eghtebas
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 37824314
Q1: put it in a js file and a method

function populateBox()
{
  var groupNames = ["Group 1", "Group 2", "Group 3", "Group 4", "Group 5"];

var select = $("<select/>");
$(groupNames).each(function() {
    select.append($("<option/>").text(this.toString()));
});

$("h1").after(select);
}

and then call this method at the ready event of document

$(document).ready(function(){
populateBox();
});

Q2: call this method after calling populateBox() in the same document ready event handler


$(document).ready(function(){
populateBox();
 $("select").onchange(function(){
    //this method will be called when any option is selected
 });
});

check this link
http://jetlogs.org/2007/09/03/jquery-select-boxes-and-change-events/
http://stackoverflow.com/questions/1280499/jquery-set-select-index/1280518#1280518
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 37824838
obviousely, I need to do some reading. But for now, could you possibly revise the html below to do the job?

<html>
  <head>
        <title>Polulate List Box</title>
  </head>

  <body>
      <h1>Polulate List Box</h1>
       
            <select name="drop1" id="Select1" size="6" multiple="multiple">
            <option value="1">Group 1</option>
            <option value="2">Group 2</option>
            <option value="3">Group 3</option>
            <option value="4">Group 4</option>

</select>

  </body>
</html>

Is there going to be one js file or two? Under what name?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37826282
you don't really need any js file...simply add all the javascript in the script tag
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 37829883
Well, I am new to this. "simply add all the javascript in the script tag" may be not very simple to me.

If possible, I prefer to have html in one file and js in another. And when I run, it will do this:

When Group 2 is selected, a message to come up saying:

You clicked on Group 2

Where you put: alert("You clicked on Group 2"); additional code will be added to perform some other tasks.

Thank you,

MIke
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

809 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