Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 12th, 2010, 09:31 AM
Authorized User
 
Join Date: Sep 2009
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default If condition on a specific value of Array

Hi to All,

I have two menus, related each other, in this way:

<head>
<Script Language="JavaScript">

var sum_db = new Object()
sum_db["email1"] = [{value:"I01 - Problems of one level", text:"I01 - Problems of one level"},
{value:"I02 - Problems RDM", text:"I02 - Problems RDM"},
{value:"Free Text", text:"Free Text "}];
sum_db["email2"] = [{value:"I03 - Various", text:"I03 - Various"}];
//
//
function setIssue(chooser) {
var newElem;
var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
var Iss_Chooser = chooser.form.elements["iss_sum"];
while (Iss_Chooser.options.length) {
Iss_Chooser.remove(0);
}
var choice = chooser.options[chooser.selectedIndex].value;
var db = sum_db[choice];
if (choice == "email1") {
newElem = document.createElement("option");
newElem.text = "Choose an issue ...";
newElem.value = "Choose Issue ...";
Iss_Chooser.add(newElem, where);
}
for (var i = 0; i < db.length; i++) {
newElem = document.createElement("option");
newElem.text = db[i].text;
newElem.value = db[i].value;
Iss_Chooser.add(newElem, where);
if ((choice == "email1") && (db[i].text.indexOf('Free Text')==0)) { document.getElementById("label_des").style.display =""; }
else { document.getElementById("label_des").style.display ="none"; }
}
}
//
</script>
</head>


<body>
<form>
<select size="1" name="mess" id="mess" onchange="setIssue(this)">
<option value="email1" selected>email1</option>
<option value="email2">email2</option>
</select>
....
....
<select id="iss_sum" name="iss_sum">
<option value="<%=objRS("Issue_Summary")%>" selected><%=objRS("Issue_Summary")%></option>
</select> <label for="iss_des" id="label_des" style="display:none; vertical-align:top">Description:<textarea id="iss_des" name="iss_des"><%=objRS("Issue_Description")%></textarea>
</label>



My problem is:
I would like that if I select ONLY email1 and ONLY the option "Free Text", appears the textarea with label Description.
Up to know if one selects email1, automatically the textarea appears near for every option; it seems that the condition of the if is not respected !!!

Thanks a lot in advance !!!
 
Old October 13th, 2010, 06:46 AM
Authorized User
 
Join Date: Sep 2009
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

The problem is:
in which way I can say:

if one choose email1 from the first menu,
and choose ONLY the option "Free Text" in the second menu,
appear a textarea .... ??

I have done this but without good result:

if ((choice == "email1") && (db[i].selected=='Free Text' )) { document.getElementById("label_des").style.display =""; }
else { document.getElementById("label_des").style.display ="none"; }
}
 
Old October 18th, 2010, 04:37 AM
Authorized User
 
Join Date: Sep 2009
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I post the solution found

I have put some alert inside if and for cicles to see the value of variables returned:

for (var i = 0; i < db.length; i++) {
newElem = document.createElement("option");
newElem.text = db[i].text;
newElem.value = db[i].value;
Iss_Chooser.add(newElem, where);
if ((choice == "email1") { alert db[i].value; }
}
alert (newElem.value); and alert on Iss_Chooser value
}

I understood that I could not solve my problem in this way,
because inside the function setIssue(chooser) it is impossible to make an if condition between a db[i] and an option value of the second menu "iss_sum" that I can select only in a following step.

So, looking at the empty value of a variable returned from alert,
I have created, inside the head, a second function called "setLabel()",
recalled in the second menu "iss_sum",
and I solved the problem.

I post now all the code to understand well all the flow:

<head>
<Script Language="JavaScript">
var sum_db = new Object()
sum_db["email1"] = [{value:"I01 - Problemi di primo livello", text:"I01 - Problemi di primo livello"},
{value:"I02 - Problemi RDM", text:"I02 - Problemi RDM"},
{value:"Free Text", text:"Free Text "}];
sum_db["email2"] = [{value:"I03 - Varie", text:"I03 - Varie"}];
//
//
function setIssue(chooser) {
var newElem;
var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
var Iss_Chooser = chooser.form.elements["iss_sum"];
while (Iss_Chooser.options.length) {
Iss_Chooser.remove(0);
}
var choice = chooser.options[chooser.selectedIndex].value;
var db = sum_db[choice];
if (choice == "email1") {
newElem = document.createElement("option");
newElem.text = "Choose an issue ...";
newElem.value = "Choose Issue ...";
Iss_Chooser.add(newElem, where);
}
for (var i = 0; i < db.length; i++) {
newElem = document.createElement("option");
newElem.text = db[i].text;
newElem.value = db[i].value;
Iss_Chooser.add(newElem, where);
}
}
//
function setLabel() {
if (document.forms[0].iss_sum.options[document.FrontPage_Form1.iss_sum.selectedIndex].text.match(/^Free/)) {
document.getElementById("label_des").style.display =""; }
else { document.getElementById("label_des").style.display ="none"; }
}
//
</script>
</head>


<body>
<form>
<select size="1" name="mess" id="mess" onchange="setIssue(this)">
<option value="email1" selected>email1</option>
<option value="email2">email2</option>
</select>
....
....
<select id="iss_sum" name="iss_sum" onchange="setLabel()">
<option value="<%=objRS("Issue_Summary")%>" selected><%=objRS("Issue_Summary")%></option>
</select> <label for="iss_des" id="label_des" style="display:none ; vertical-align:top">Description:<textarea id="iss_des" name="iss_des"><%=objRS("Issue_Description")%></textarea>
</label>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Update specific field at specific time sanchougule PHP Databases 1 August 12th, 2009 03:16 PM
Search specific data from specific columns yogeshyl SQL Language 1 January 16th, 2008 11:12 AM
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
Copy specific data in specific cells of sheet2 yogeshyl Excel VBA 1 May 14th, 2007 07:40 AM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.