Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-13-2012, 01:16 AM   PM User | #16
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yep. Worked fine.

Code:
<html>
<body>
<script type="text/javascript">

function calculateName(name)
{
    return  (name.toUpperCase() + "XXX" ).replace( /[aeiou]/ig, "" ).substring(0,3);
}
function calculateName2(name)
{
   return (name.charAt(0) + (name + "XXX").substring(1).replace(/[aeiou]/ig,"") ).substring(0,3).toUpperCase();
}

function demo( word )
{
    document.write( word + "==>> (1) " + calculateName(word) + ", ==>> (2) " + calculateName2(word) + "<hr/>" );
}

demo("zamboni");
demo("alphabet");
demo("zoo");
demo("oui");
</script>
</body>
</html>
Note: Both functions work for words of at least 1 letter. The second one will fail if given a word with no characters. The first one will produce "XXX".
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 11-13-2012 at 01:18 AM..
Old Pedant is offline   Reply With Quote
Old 11-14-2012, 07:59 PM   PM User | #17
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Yep. Worked fine.

Code:
<html>
<body>
<script type="text/javascript">

function calculateName(name)
{
    return  (name.toUpperCase() + "XXX" ).replace( /[aeiou]/ig, "" ).substring(0,3);
}
function calculateName2(name)
{
   return (name.charAt(0) + (name + "XXX").substring(1).replace(/[aeiou]/ig,"") ).substring(0,3).toUpperCase();
}

function demo( word )
{
    document.write( word + "==>> (1) " + calculateName(word) + ", ==>> (2) " + calculateName2(word) + "<hr/>" );
}

demo("zamboni");
demo("alphabet");
demo("zoo");
demo("oui");
</script>
</body>
</html>
Note: Both functions work for words of at least 1 letter. The second one will fail if given a word with no characters. The first one will produce "XXX".
Ok thanks Guy
Now please, Can you tell me how to use a radio button and his function checked true, false? Because in www.w3schools.com I don't find how to use it!!!!
triko is offline   Reply With Quote
Old 11-14-2012, 08:47 PM   PM User | #18
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by triko View Post
Ok thanks Guy
Now please, Can you tell me how to use a radio button and his function checked true, false? Because in www.w3schools.com I don't find how to use it!!!!
Not sure if w3schools would even cover how to do that since the javaScript covered there ismostly the antiquated version for Netscape 4 and earlier. The two guys who wrote the site in their spare time tried to cover so many different topics that they are struggling to keep any of them up to date and JavaScript has undergone enormous changes since they wrote their site and while they have added pages on the new commands they haven't updated the existing pages to reflect the changes in how they should be used.

To be able to make your code interactive you will first need to get rid of the document.write statement and replace it either with innerHTML calls or proper Document Object Model calls.

To attach processing to elements in the web page you need to be able to identify the element from the JavaScript (giving it an id and using getElementById is easiest but there are lots of other ways) and then attach an appropriate event handler or event listener to the element to specify what code to run when a specific event occursa on that element.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-15-2012, 05:01 PM   PM User | #19
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,393
Thanks: 18
Thanked 351 Times in 350 Posts
sunfighter is on a distinguished road
It's not easy, but here's how to find things on w3school:
Main page http://www.w3schools.com/ on right is "Web References" Find and click JavaScript. Bottom of page under "HTML DOM Objects Reference" is "Input Checkbox object" click. Your on the Checkbox Object page and under "Checkbox Object Properties" is "checked" On that page is an example on setting the box, getting the state of the box and a link to a "Try it yourself" page.
sunfighter is online now   Reply With Quote
Old 11-15-2012, 11:20 PM   PM User | #20
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
Not sure if w3schools would even cover how to do that since the javaScript covered there ismostly the antiquated version for Netscape 4 and earlier. The two guys who wrote the site in their spare time tried to cover so many different topics that they are struggling to keep any of them up to date and JavaScript has undergone enormous changes since they wrote their site and while they have added pages on the new commands they haven't updated the existing pages to reflect the changes in how they should be used.

To be able to make your code interactive you will first need to get rid of the document.write statement and replace it either with innerHTML calls or proper Document Object Model calls.

To attach processing to elements in the web page you need to be able to identify the element from the JavaScript (giving it an id and using getElementById is easiest but there are lots of other ways) and then attach an appropriate event handler or event listener to the element to specify what code to run when a specific event occursa on that element.
AN ok! Thanks for info! So my function checked have simple use: if female ad 40 at number of day date that i0m insert on text type, if male don't add anything and write only the number of day
triko is offline   Reply With Quote
Old 11-15-2012, 11:48 PM   PM User | #21
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ummm...but you *ASKED* for
Quote:
Can you tell me how to use a radio button and his function checked true, false?
And that's not as simple.

A normal radio button, if there is only ONE of the given name=, can *NOT* be unchecked. Only checked.

You would need some mildly sneaky JavaScript code to be able to un-check a single radio button.

I don't think that's what you really wanted, based on your last post, but just be aware of it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-16-2012, 12:09 PM   PM User | #22
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Ummm...but you *ASKED* for
And that's not as simple.

A normal radio button, if there is only ONE of the given name=, can *NOT* be unchecked. Only checked.

You would need some mildly sneaky JavaScript code to be able to un-check a single radio button.

I don't think that's what you really wanted, based on your last post, but just be aware of it.
I badly explained, re ask my question
So my function day contains the document.getElementById (*day that i have selected from menù select*) and if select radio female button, add 40 at number that i selected from menù, else if I have selected male write only the number that I take from menù
I would like this method
Code:
<script>
function day (myDay)
{
Now write that I think :)
if (id = "male" == checked false)
{
var number;
number = myDay + 40;
}
else (id = "male" == checked true)
{
var number;
number = myDay;
}
alert (myDay);
}
</script>
<body>
<input type = "radio" name = "sex" id = "male" checked = "false" />
<input type = "radio" name = "sex" id = "female" checked = "true" />
Day: <select id = "myDay">
       <option>1</option>
       <option>2</option>
       </select>
triko is offline   Reply With Quote
Old 11-16-2012, 08:44 PM   PM User | #23
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
WOW! You know, you can *NOT* just make up your own language. You have to use the language AS IT EXISTS.
Code:
function day(myDay)
{
    var num = myDay;
    if ( document.getElementById("female").checked ) { num += 40; }
    alert( num );
}
CAUTION: This *ASSUMES* that if "female" is *NOT* checked, then "male* *IS* checked.

If you want to be careful about it:
Code:
function day(myDay)
{
    var num = "You did not check either radio button!";
    if ( document.getElementById("male").checked ) { num = myDay; }
    if ( document.getElementById("female").checked ) { num = myDay + 40; }
    alert( num );
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-16-2012, 09:37 PM   PM User | #24
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
WOW! You know, you can *NOT* just make up your own language. You have to use the language AS IT EXISTS.
Code:
function day(myDay)
{
    var num = myDay;
    if ( document.getElementById("female").checked ) { num += 40; }
    alert( num );
}
CAUTION: This *ASSUMES* that if "female" is *NOT* checked, then "male* *IS* checked.

If you want to be careful about it:
Code:
function day(myDay)
{
    var num = "You did not check either radio button!";
    if ( document.getElementById("male").checked ) { num = myDay; }
    if ( document.getElementById("female").checked ) { num = myDay + 40; }
    alert( num );
}
Ok, sorry but I don't know how take to input checked!!! And it's so Easy, only add getElementById ("").checked Thanks so much my mentor Master javascript!
triko is offline   Reply With Quote
Old 11-16-2012, 09:45 PM   PM User | #25
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Sorry Pedant, but what mean:
var num = "You didn't check either radio button!";
???
triko is offline   Reply With Quote
Old 11-16-2012, 10:13 PM   PM User | #26
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
That means they did not check EITHER Male OR Female.

BOTH buttons were NOT CHECKED.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-16-2012, 10:42 PM   PM User | #27
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
That means they did not check EITHER Male OR Female.

BOTH buttons were NOT CHECKED.
And what I need to check whether it is male or female?
I know tht is female check
triko is offline   Reply With Quote
Old 11-16-2012, 11:13 PM   PM User | #28
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
*TRY* the code I gave you.

It looks for *BOTH* Female and Male and gives the right answer if EITHER ONE is checked.

ONLY if NONE are checked does it give that other message.

Or use the FIRST code I gave you instead of the second code.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-20-2012, 02:19 PM   PM User | #29
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
*TRY* the code I gave you.

It looks for *BOTH* Female and Male and gives the right answer if EITHER ONE is checked.

ONLY if NONE are checked does it give that other message.

Or use the FIRST code I gave you instead of the second code.
OK thanks guy <3
triko is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:07 AM.


Advertisement
Log in to turn off these ads.