 |
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
|
|
|

September 14th, 2008, 02:05 AM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Comma Versus Dot
I have a problem with my javascript, my question is how should I write if I dont want my last (select[i].checked) have a comma but replace with dot in front.
Example :
You are interested in : Programmes Offered, Short Courses, Visiting.
I cannot get what I want with the script below where the output came out with all comma in front :-
" You are interested in : Programmes Offered, Short Courses, Visiting,"
<SCRIPT LANGUAGE="JavaScript">
function tick()
{
var select = document.forms[0].selection;
var txt = "";
var num=0;
for (i=0; i < select.length; ++i)
{
if (select[i].checked)
{
++num;
txt = txt + num;
txt = txt + ".";
txt = txt + "";
txt = txt + select[i].value;
txt = txt + ", ";
}
}
window.alert("You are interested in : " + txt);
}
</SCRIPT>
|

September 14th, 2008, 02:40 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You can use JavaScript methods like replace and indexOf to find the last comma and replace it. However, it may be even easier to check where you are inside the loop and add a period for the last item:
if (i < select.length - 1)
{
txt = txt + ", ";
}
else
{
txt = txt + ".";
}
This way, when i is not less than the length of the list minus 1 (that is, i == select.length -1, the last item in the list) a dot is added instead of a comma.
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|

September 15th, 2008, 04:07 AM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Imar for your kindly responded, but I used select[i].checked instead of select.length.
Whatever item tick in my checkbox, stored in array and will display after clicking a button (onClick Function).
Example
<BUTTON TYPE="BUTTON" VALUE="Process" onClick="tick()"> Process </BUTTON>
So I dont have any fix length. Any idea ?
Here I code for U...may be you can get a clear picture...paste it at notepad and run as html.
<HTML>
<HEADER>
<TITLE>
Imar Spaanjaars not unique
</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function tick()
{
var select = document.forms[0].selection;
var txt = "";
var num=0;
for (i=0; i < select.length; ++i)
{
if (select[i].checked)
{
++num;
txt = txt + num;
txt = txt + ".";
txt = txt + "";
txt = txt + select[i].value;
txt = txt + ", ";
}
}
window.alert("You are interested in : " + txt);
}
</SCRIPT>
</HEADER>
<BODY>
How can we help you ?
<BR>
<FORM>
<INPUT TYPE=CHECKBOX NAME="selection" VALUE="Program offered" > I'm interested in your program offered
<BR>
<INPUT TYPE=CHECKBOX NAME="selection" VALUE="Short courses" > I'm interested in short courses
<BR>
<INPUT TYPE=CHECKBOX NAME="selection" VALUE="Visiting our campus" > I'm interested in visiting your campus
<BR><BR>
<BUTTON TYPE="BUTTON" VALUE="Process" onClick="tick()"> Process </BUTTON>
</FORM>
</BODY>
</HTML>
|

September 15th, 2008, 04:17 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
In that case, you could add the comma *before* each item (except for the first).
Untested:
Code:
for (i=0; i < select.length; ++i)
{
if (select[i].checked)
{
++num;
if (txt.length > 0)
{
txt = txt + ", ";
}
txt = txt + select[i].value;
}
}
// Now add the .
if (txt.length > 0)
{
txt = txt + ".";
}
window.alert("You are interested in : " + txt);
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|

September 15th, 2008, 01:31 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Or you can do it more efficiently:
Code:
var num = 0; // not sure why this was added, but since it was...
for (var i=0; i < select.length; ++i)
{
if (select[i].checked)
{
++num; // we will use it...
txt += ( ", " + select[i].value );
}
}
// Now strip the first ", " and add the . taking advantage of num variable:
if ( num == 0 ) txt = "[nothing at all]";
else txt = txt.substring(2) + ".";
window.alert("You are interested in : " + txt);
|

September 16th, 2008, 02:06 AM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I got it now...thanks Imar and Old Pedant for the help.
|

March 1st, 2011, 03:37 AM
|
Registered User
|
|
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I had modify a little bit code above... but cannot run well.... please guide me whats wrong ??? TQ
Code:
<HTML>
<HEAD>
<TITLE>
Question B : HTML and Javascript Example
</TITLE>
<script type="text/javascript">
function validate(formCheck) //Function with a parameter representing a form name.
{
if (formCheck.name.value == "")
{
alert("Please provide your name:");
formCheck.name.focus();
return false;
}
var mail = formCheck.email.value
if (mail.indexOf("@.") == -1)
{
alert("Invalid Email");
formCheck.email.focus();
return false;
}
return true;
}
</script>
</HEAD>
<BODY>
<FORM name="inform" action="" method="post" onClick="return validate(inform)";>
Your Name: <INPUT type=text NAME="name" value="" SIZE=30><br>
Your Email: <INPUT type=text NAME="email" value="" SIZE=30>
<INPUT type="button" name="submit" value="Validate" >
</FORM>
</BODY>
</HTML>
<HTML>
<HEAD>
<script type="text/javascript">
function tick()
{
var select = document.forms[0].selection;
var txt = "";
var num=0;
for (i=0; i < select.length; ++i)
{
if (select[i].checked)
{
++num;
txt = txt + num;
txt = txt + ".";
txt = txt + "";
txt = txt + select[i].value;
txt = txt + ", ";
}
}
window.alert("You are interested in : " + txt);
}
</script>
</HEAD>
<BODY>
How can we help you ?
<BR>
<FORM>
<INPUT TYPE=CHECKBOX NAME="selection" VALUE="Program offered" > I'm interested in your program offered
<BR>
<INPUT TYPE=CHECKBOX NAME="selection" VALUE="Short courses" > I'm interested in short courses
<BR>
<INPUT TYPE=CHECKBOX NAME="selection" VALUE="Visiting our campus" > I'm interested in visiting your campus
<BR><BR>
<BUTTON TYPE="BUTTON" VALUE="Process" onClick="tick()"> Process </BUTTON>
</FORM>
</BODY>
</HTML>
|
|
 |