3 Replies - 418 Views - Last Post: 24 August 2016 - 06:07 AM

#1 needhelppls123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-August 16

How do I make it so the button works as a function?

Posted 23 August 2016 - 04:47 PM

Hello every1, was just wondering how I can enable this button to work. I need to print out the Zodiac sign and input random responses but I will do that myself, I just need advice so when I press the button it prints out the Zodiac sign, thank you in advance :).

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>


<body>
<p> SoothSayer
<br/>
<form>
Name: <input type="text" name="textfield" id="Name"><br>
Birthday: <select id="month">
        <option value=0>&nbsp&nbsp--Month--</option>
        <option value=1>January</option>
        <option value=2>February</option>
        <option value=3>March</option>
        <option value=4>April</option>
        <option value=5>May</option>
        <option value=6>June</option>
        <option value=7>July</option>
        <option value=8>August</option>
        <option value=9>September</option>
        <option value=10>October</option>
        <option value=11>November</option>
        <option value=12>December</option>
    </select> 
    <select id="date">
        <option value=0>&nbsp&nbsp--Date--</option>
    </select>
    </form>
<input type="button" onclick="myFunction()" value="Submit">
</p>
</form>
<script>
var dsel = document.getElementById("date");
var msel = document.getElementById("month");

dsel.options.length = 32;
for ( var d = 1; d <= 31; ++d ) {
dsel.options[d] = new Option(d,d);
}

msel.onchange =  clearbox;
function clearbox() {
document.getElementById("Submit").innerHTML = " ";
document.getElementById("date").selectedIndex = 0;
}

dsel.onchange = getSubmit;

function getSubmit( ) {
var m = Number(msel.value);
var d = Number(dsel.value);
var s = "";
if ((d == 0) || (m == 0)) {
document.getElementById("Submit").innerHTML = " ";
return;
}

    var md = m * 100 + d;
    // if Jan 1 to Jan 19, pretend it is late December
    if ( m == 1 && d > 0 && d <= 19 ) md = 1299;

    if ( md >= 1222 )      { s = "Capricorn"; } 
    else if ( md >= 1122 ) { s = "Sagittarius"; } 
    else if ( md >= 1023 ) { s = "Scorpio"; } 
    else if ( md >= 923 )  { s = "Libra"; } 
    else if ( md >= 823 )  { s = "Virgo"; }
    else if ( md >= 723 )  { s = "Leo";   }
    else if ( md >= 622 )  { s = "Cancer"; }
    else if ( md >= 521 )  { s = "Gemini"; }
    else if ( md >= 420 )  { s = "Taurus"; }
    else if ( md >= 321 )  { s = "Aries"; }
    else if ( md >= 219 )  { s = "Pisces"; }
    // last test is for 100:  Both MM and DD must be > 0 to trigger this
    else if ( md >  100 )  { s = "Aquarius"; }

    document.getElementById("Submit").innerHTML = "Your Zodiac sign is " + s;
}

</script>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: How do I make it so the button works as a function?

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,269
  • Joined: 03-December 12

Re: How do I make it so the button works as a function?

Posted 23 August 2016 - 05:33 PM

First question, where is your myFunction, function?
Was This Post Helpful? 0
  • +
  • -

#3 laytonsdad   User is offline

  • Let it rip!
  • member icon

Reputation: 466
  • View blog
  • Posts: 1,992
  • Joined: 30-April 10

Re: How do I make it so the button works as a function?

Posted 23 August 2016 - 07:15 PM

Quote

How do I make it so the button works as a function?


The fastest way to answer this is:

1) Create a function

function logMe(){
  console.log("I have been logged!");
}



2)Create element to click

<button id="logText" type="button">Click to Log</button>



3)Create event listener

//Add an event Listener for the button with the id of "logText" to listen for the click event to be triggered. Then it will call the logMe function when the click event is triggered.
logBtn.addEventListener("click", logMe);



Hope this gives a good example!
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7197
  • View blog
  • Posts: 15,004
  • Joined: 16-October 07

Re: How do I make it so the button works as a function?

Posted 24 August 2016 - 06:07 AM

My google fu is strong today.

Code plagiarized from here: http://www.codingfor...p-code-pls.html

Perhaps if you write your own, you'll have a better idea how it works? This sort of thing is not doing yourself any favors.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1