function on a checkbox? **noob**

Discussion in 'JavaScript' started by Light Speed, Oct 23, 2009.

  1. #1
    how would i go about making a function on a checkbox that pops up one alert menu when its checked and pops a different alert when its unchecked?

    here's what i have so far i know its wrong i dont know how to change the beginning
    script:
    
    function compare(bing)
    {
    if ("bing"=checked)
      {
      alert("You agree, your favorite search engine is also bing.");
      }
    else
      {
      alert("you disagree, your favorite search engine is not bing.");
      }
    }
    Code (markup):

    later in the html i have this button named bing

    <input type="checkbox" checked name="bing" value="Click Me" onClick="message()"></input>
    Code (markup):

     
    Light Speed, Oct 23, 2009 IP
  2. VladimirS

    VladimirS Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try something like this

    <input type="checkbox" checked="checked" name="bing" value="Click Me" onClick="message()"></input>
    Code (markup):
    function message() {
        if (document.myform.bing.checked) {
             alert("checked");
        }
        else {
             alert("unchecked");
        }
    }
    Code (markup):
     
    VladimirS, Oct 23, 2009 IP
  3. Light Speed

    Light Speed Member

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    edit i think i gave you the wrong info
     
    Light Speed, Oct 23, 2009 IP
  4. Light Speed

    Light Speed Member

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    what about a button

    how would i go about adding a button that pops up the specific alert (determined by checked box from the compare function ) when clicked
    this is what i have so far
    <input type="button" value="Check Me" onClick="compare()" >
    Code (markup):
    i want that button to pop up the specific alert (if check box is checked, pop up the "yes" alert, if its not checked, pop up the "no" alert) when clicked
     
    Light Speed, Oct 23, 2009 IP
  5. VladimirS

    VladimirS Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can use the same code.

    document.myform.bing returns form element regardless of the event which triggers the function.
     
    VladimirS, Oct 23, 2009 IP