Solved

js html hookup...

Posted on 2012-04-09
4
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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 34

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

691 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