Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

js html hookup...

Posted on 2012-04-09
4
Medium Priority
?
289 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:
Gurvinder Pal Singh earned 2000 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:Gurvinder Pal Singh
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

604 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