Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14
  1. #11
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1

    Quote Originally Posted by Deathnote View Post
    oh this is definitely better...tnx bro...so you can have a function with a capital first letter but not have the same id and name... i thought Add and Add() was different...damn....oh yeah found out that isnan is to check if it is string or not..wow im learning alot from you guyz


    but last question i promise...where did val come from? i mean its neither the id nor the name of the check box yet it was used that way...does the control that called the function when you put val on the parameter becomes the value for val? tnx
    val = is just an "argument" (in programming terms) , it holds any type of value/object, as soon as you call your function. the way you call your function depends upon who you declare it.

    eg.

    Declaration : function add(x,y)
    Function call : add(1,2)

    Declaration : function display(name,age)
    Function call: display("mark", 20)


    [/quote]
    in my example:

    <input type=checkbox Id="idAdd" onClick="fnAdd(this)">

    "this" is an "object" (checkbox) having a method "Value" (True or False)
    in OOP you can call thi commonly <OBJECT>.<ATTRIBUTES> OR <OBJECT>.<METHODS>
    Methods are also considered as an attribute, however it performs a certain operation.
    you can learn this as soon as you study OOP concept.

    try to change the previous lines to this (highlighted as red) or just copy&paste the code below:

    <html>
    <head>
    <script type="text/javascript">

    function fnAdd(objCBox, myString){
    if (objCBox.checked) {

    sum =parseFloat(calculator.input1.value) + parseFloat(calculator.input2.value);
    if (!isNaN(sum)) document.getElementById("output").innerHTML= myString + sum
    else document.getElementById("output").innerHTML= "Invalid data."
    }
    else {
    document.getElementById("output").innerHTML= ""
    }
    }
    </script>
    </head>
    <body>
    <form name=calculator>
    <input type=checkbox Id="idAdd" onClick="fnAdd(this, 'Total: ')">
    <input type=text name=input1>
    <input type=text name=input2>
    <div id=output> </div>
    </form>
    </body>
    </html>
    Last edited by MarkCuering; 01-19-2009 at 09:00 PM.

  2. #12
    when using name of the element use this -->Add.checked <input type="checkbox" name="Add">

    when using id of the element use this --> document.getElementById("Add").checked==True)
    <input type="checkbox" id="Add">

    parehas ra na sila ug function..

    kasabot!!!! payts

  3. #13
    Hmm mura'g layo ra gituyukan ang topic .. actually ok ra imo first code, all it needed are 2 small fixes. To demonstrate a point i'll explain later, i changed the function name to ADD (take note, all CAPS)

    Code:
      1.     function ADD()
      2.     {
      3.         x=calculator.input1.value;
      4.         y=calculator.input2.value;
      5.         sum= x + y;
      6.         if (document.getElementById("Add").checked==true)
      7.         document.getElementById("output").innerHTML= sum;
      8.         else
      9.         document.getElementById("output").innerHTML= " ";
     10.     }
     11.     
     12.     ...
     13.     <input type=checkbox Id="Add" onClick="ADD()">
     14.     ...
    </script>
    The fixes are:
    line 1 - Add -> ADD
    line 6 - True -> true
    line 13 - Add -> ADD

    1) sa javascript, lahi ang ADD sa Add kay cAsE-sEnSeTiVe man ni nga language. what you had in your first code was a clash in identifier names. this happens kay sa javascript everything are first-class objects (pero lahi na na nga topic)

    2) True isn't valid .. any idea why?


    Of course in real programming "ok ra" is a bad mindset. You have to be defensive and give careful attention to input verification. That's why parseFloat and isNaN came up in the discussion. Ang point lang nako we almost missed learning subtle points from your first code and the fact that it could have "worked" kay natabunan na sa ubang concerns

    so anyway, this is how i would code this exercise-

    Code:
    <html>
    <head>
    <script type="text/javascript">
    window.onload = function() {
        document.getElementById('Add').onclick = function () {
            x = parseFloat(calculator.input1.value);
            y = parseFloat(calculator.input2.value);
            if (!x || !y) {
                alert('Gai sad ta numbers iadd oi.');
                return;
            }
            
            sum = x + y;
            
             // ok ra to imoha gamit ang "if" kung maoy gusto ni prof
            document.getElementById('output').innerHTML = this.checked ? sum : '';
        }
    }
    </script>
    
    
    </head>
    
    
    <body>
    <form name=calculator>
    <input type=checkbox Id="Add">
    <input type=text name=input1>
    <input type=text name=input2>
    <div id=output> </div>
    </form>
    </body>
    </html>

  4. #14
    guyz new problem....please please please read first post...tnx

  5.    Advertisement

Page 2 of 2 FirstFirst 12

Similar Threads

 
  1. WHAT'S WRONG WITH THE WORLD TODAY? Let's fight for righteousness
    By illuminatus in forum General Discussions
    Replies: 13
    Last Post: 05-03-2012, 08:00 AM
  2. WHATS WRONG WITH MY PC- upgrading the rig and its downside
    By benchkicker in forum Computer Hardware
    Replies: 74
    Last Post: 09-09-2007, 12:47 AM
  3. whats wrong with my photoshop?
    By estor_boot in forum Software & Games (Old)
    Replies: 10
    Last Post: 07-25-2007, 02:46 PM
  4. Whats wrong with my CD-rom?
    By pobre in forum Computer Hardware
    Replies: 8
    Last Post: 11-30-2005, 07:47 PM
  5. whats wrong with my monitor?
    By jonmlas in forum Computer Hardware
    Replies: 8
    Last Post: 10-22-2005, 02:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top