JavaScript - Simplified An Easy Function
hi guys,
this is a simple js ,there are 3 color block, when the mouse hover over one of them ,other one the forth block's color change, acorrding to the 3 color block.i want to simplified this js,cause the color repeat twice. Code: <html> <head> <script type="text/javascript" > function tdchangeColor(color){ document.getElementById("input").style.backgroundColor=color } </script> </head> <body> <div style="width:30px;height:30px;background:red;float:left;" onmouseover="tdchangeColor('red')"></div> <div style="width:30px;height:30px;background:blue;float:left;" onmouseover="tdchangeColor('blue')"></div> <div style="width:30px;height:30px;background:green;float:left;" onmouseover="tdchangeColor('green')"></div> <br><br> <input type="text" id="input"> </body> </html> Similar TutorialsI'm coding a simple random sentence generator & this is the beginning part of what I'm trying to accomplish. I created 3 different functions with 3 separate buttons & 3 separate text areas to display the noun, verb, or article once the button is pressed. However, when I press a button, nothing shows up in the text area! Can someone please tell me what I'm doing wrong & give me a fix? It has to be a simple problem, but I am very very new to this & need help. Thanks in advance! Code: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script language="JavaScript"> function noun() // Assumes: nothing // Returns: a random noun { return RandomOneOf(["man", "woman", "ball"]); } </script> <script language="JavaScript"> function verb() // Assumes: nothing // Returns: a random noun { return RandomOneOf(["hit", "liked", "smelled"]); } </script> <script language="JavaScript"> function article() // Assumes: nothing // Returns: a random noun { return RandomOneOf(["the", "a", "some"]); } </script> </head> <body> <input type="button" value="Noun" onclick="document.getElementById('OutNoun').value = noun();" /> <TEXTAREA ID="OutNoun" ROWS=1 COLS=15 WRAP VALUE=""></TEXTAREA> <input type="button" value="Verb" onclick="document.getElementById('OutVerb').value = verb();" /> <TEXTAREA ID="OutVerb" ROWS=1 COLS=15 WRAP VALUE=""></TEXTAREA> <input type="button" value="Article" onclick="document.getElementById('OutArticle').value = article();" /> <TEXTAREA ID="OutArticle" ROWS=1 COLS=15 WRAP VALUE=""></TEXTAREA> </body> </html> Short version at bottom Need to add an array before an array to init thousands of arrays. Mostly worked out, just need to add an array before an array. ........Please check it out. Really want to finish this tonight or tomorrow. Alright, so two days later I finally have this portion working but its only because of the awesome people of this forum and unforunately these people thought I had some half decent grasp of javascript (which I don't) and so their answers were meant to solve my problem but each time I was left with no idea how to repeat what they did. So, I've learned a lot of extra stuff that I really could have done without in the effort to try and understand what they did. This is all well and good because I'm much farther than I am had I gone it alone (so thank you!) but please, anyone that posts an answer, could you try and explain a bit of how I might use your solution again. For example, today I was confused for about an hour because I didn't understand how [CODE]var newArray=[], a, i=0;CODE] worked but only after staring at it long enough and not finding anything on google related to "values inputed after array initialization" did I finally realized that these were not params of a new array but just new variables. Code: var alphabetArray =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','all1']; var a; var i=0; while(a=alphabetArray[i++]){ eval('var _img_'+a+' = []'); eval('var _h_'+a+' = []'); eval('var _r_'+a+' = []'); eval('var _m_'+a+' = []'); eval('var _yt_'+a+' = []'); } alert(_h_all1) and its working perfectly but I somehow I need to add Code: var pageNum = ['','p2_','p3_','p4_','p5_','p6_','p7_','p8_','p9_','p10_'] need to add pageNum to every alphabetArray so... _img_a = [],_img_p2_a = [], img_p3_a = [].... _img_p10_a = [] repeated for every variable in alphabetArray. _yt_p10_all1=[] Super short version Code: var alphabetArray =[letters/numbers]; var a; var i=0; while(a=alphabetArray[i++]){ eval('var _img_'+a+' = []'); } // works now I need to add 10 page prefixes before each var in alphabetArray Code: var pageNum = ['','p2_','p3_','p4_','p5_','p6_','p7_','p8_','p9_','p10_'] need to add pageNum to every alphabetArray so... _img_a = [],_img_p2_a = [], img_p3_a = [].... _img_p10_a = [] repeated for every variable in alphabetArray. _yt_p10_all1=[] Please explain how this might be possible in a way that your dog might understand. I have written the following code which returns a simple times table. The problem is that I want the resulting times table written to the div id=results section in the body of the html page rather than writing it to another page. I hope that makes sense! Thanks in advance for any help. <html> <head> <script type="text/javascript"> function timestable() { var num = parseInt(document.timesForm.times.value); var counter = 1; while (counter<=12) { document.write(counter+" X "+num+" = "+counter*num+"<br />"); counter++; } } </script> </head> <body> <form id="timesForm" name="timesForm"> <p>Enter the number he <input type="text" id="times" /></p> <p> <input type="button" value="Show Times Facts" onclick="timestable()" /> </p> <div id="results">Times Facts</div> </form> </body> </html> hey guys im trying to make something that selects two different images but it keeps selecting two images that are the same, and dont know how to go about it to make it select two different. please help Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/javascript; charset=utf-8" /> <title>Untitled Document</title> <script language="JavaScript"> <!-- var theImages = new Array() theImages[0] = '100.jpg' theImages[1] = '200.JPG' theImages[2] = '300.JPG' theImages[3] = '400.jpg' var j = 0 var p = theImages.length; var preBuffer = new Array() for (i = 0; i < p; i++){ preBuffer[i] = new Image() preBuffer[i].src = theImages[i] } var whichImage = Math.round(Math.random()*(p-1)); function showImage(){ document.write('<img src="'+theImages[whichImage]+'">'); } //--> </script> </head> <body> <div align="center"></div> <table width="996" height="511" border="1"> <tr> <th scope="col"><script language="JavaScript"> <!-- showImage(); //--> </script> or <script language="JavaScript"> <!-- showImage(); //--> </script> </th> </tr> </table> </body> </html> This is javascript that turns your current url into a tinyurl. How would I go about getting this to open into a new tab instead of the same tab? Code: javascript:void(location.href='http://tinyurl.com/create.php?url='+encodeURIComponent(location.href)) I'm very very new to javasctipt/html programming and have come upon a stump.Let me explain Code: <form name="for1"> <input type="radio" name="fr1"value="1" > 5 <br> <input type="radio" name="fr1"value="2" > Όσα τα pixels<br> <input type="radio" name="fr1"value="3" > Δεν υπάρχει όριο<br> </form> <input type="button" value="Εκχώρηση Απάντησης" onClick="temp=document.for1.fr1.value"> What i was hoping with this is to store the value of the selected radio in a variable.The variable is declared at the beginning (not shown here).I would appreciate greatly if i'm on the right track to please tell me how to make this work and if possible not create functions and other complicated procedures as it will take me out of my "learning curve".Thanks in advance I don't know if this is in the correct section, sorry if it is not. I am creating a form in mailchimp for my site, I need to know if I can put the following all on one line instead of "last name" on one line and "input box" on another line. I know this is probably an easy fix for the pro's! <div class="mc-field-group" style="margin: 1.3em 5%;clear: both;overflow: hidden;"> <label for="mce-LNAME" style="display: block;margin: .3em 0;line-height: 1em;font-weight: bold;">Last Name <strong class="note-required">*</strong> </label> <input type="text" value="" name="LNAME" class="required" id="mce-LNAME" style="margin-right: 1.5em;padding: .2em .3em;width: 95%;float: left;z-index: 999;"> </div> Hey! Sorry if this is a very remedial question, but I'm trying to make a table, of even and odd numbers organized in two columns (with as many rows as it takes to get through the sequence) and I'm having an issue with my code, obviously - haha. I'm still slightly new.. here is my code Code: document.write('<table>'); for (i=1; i < 50; i++) { document.write("<tr>"); for (j = 0; j < 50; j++) { if (j % 2==0) { document.write('<td>' + j + '</td>'); } else { document.write('<td>' + i + '</td>'); } } } document.write("</tr>); document.write("</table>"); Any help would be greatly appreciated! I don't know much about programming but I can't figure out how this works and I want to implement it on my site. I want to copy the code from this page sdapc.com/billing/pay-online.html and implement something similar. I understand that the form uses post and then goes to form-action.php and the information is verified using the script.js, but how is the information sent anywhere. I mean, I don't see any reference to any script that compiles the data and sends it to the billing department. How do they get the info??? Thanks in advance. P.S. If anyone knows of similar scripts or methods out there of doing this I'm all ears (or eyes) This would just be temporary until full automation is developed. For now, I don't mind entering their info into PayPal myself. hi guys, l am trying to learn javascript, although certain things seem to be easy to understand, l still have plenty of questions. so l was reading some articles on w3schools and l was an interesting table about operations. http://i1087.photobucket.com/albums/...kesnosense.png if you check above's table you will see at the increment section as well decrement , you will see that in some cases the x and y dont have the same value, now l was just wondering, what is the difference between x=++y and x=y++ ? I am just learning javascript. I am playing around with different html elements and document.write. I have a HTML page with elements h1, h2, h3, and h4. I want to write something (in this example "found it") next to the elements. <head> <script type="text/javascript" language="javascript"> var headerList = getElementsByTagNames('h1,h2,h3,h4'); if (headerList) { document.write("Found it!"); } </script> </head> <body> <h1> </h1> <h2> </h2> <h3> </h3> <h4> </h4> </body> Why doesnt this work? I am assuming my logic is flawed? hey i just started javascript, i have a little syntax question: <HTML> <HEAD> <script type="text/javascript"> function ifPrime(input) { thesqrt = Math.sqrt(input) for(a = 2; a <= thesqrt; a++) { if(input % a == 0) return false; } return true; } function nthPrime(input) { for(i = 2; i <= input; i++) { if( ifPrime(i) ) { document.write(i); document.write("<br />"); } } } </script> </HEAD> <BODY> Enter how many primes <INPUT TYPE=TEXT NAME="userInput" SIZE=6> <INPUT TYPE=BUTTON VALUE="Do it" ONCLICK="value = userInput"> <br /> <br /> <script type="text/javascript"> nthPrime(value) </script> </BODY> </HTML> i would like it post 2 3 5 7 when i type in 10 and press the button. how do I do that? any other programing tips would be appreciated I am trying to create a javascript program that will assign a number based on a score. If the grade (number) is 90 or above it assigns a 4, if it is in the 80's its assigned a 3, etc. So far: Code: <<?xml version = "1.0" encoding = "utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> ITS200 Project 6: Quality Points </title> <script type = "text/javascript"> <!-- function QualityPoints() { var form = document.getElementById( "form" ); var input = parseInt( form.number.value); form.points.value = (input >= 0 && input <= 100) Number(input): "Error"; } // end function function Number(grade) { if (grade >= 90) return 4; else if (grade >=80) return 3; else if (grade >=70) return 2; else if (grade >=60) return 1; else return 0; } // end Number function // --> </script> </head> <body> <form id = "form" action = ""> <table border = "1"> <tr> <td> Enter Grade </td> <td> <input name = "number" type = "text" /> </td> <td> <input type = "button" value = "Get Quality Points" onclick = "QualityPoints()" /> </td> </tr> <tr> <td> Quality Points </td> <td> <input type = "text" name = "points" /> </td> </tr> </table> </form> </body> </html> Where do I go from here? I have no idea whats wrong. Below is a mock snippet of what I need. I think the issue is Code: document.write(b) because if I change that to an alert, it works fine. I thought I was using the proper syntax, but I guess I am not. Can anyone help a noob? Code: <html> <head> <script type="text/javascript"> function d(){ a=document.getElementById("txt1").value; if(a > 1){ b=a/100; document.write(b) } else{ b=a; alert(b) } } </script> </head> <body> <form name="form"> <h2>This is a form</h2> <input type="text" id="txt1" name="txt"/> type here <br /> <button type="submit" id="btn" onclick="d()">Submit</button> </body> </html> Hello, I have a script that makes a link a different color on click, i would like to add a hover color to the links that are NOT active. Script below. Thanks in advance! Code: function activate(el) { var links=document.getElementsByTagName("a"); for(i=0;i<links.length;i++) { if(links[i].className=="toggle") { links[i].style.color="#999"; } } el.style.color="#DF8700"; } i have a script that will let you know what the date is.. as well as a countdown to thanksgiving.. it is called tday.js Code: function showDate() { thisDate = new Date(); var thisWDay=thisDate.getDay(); var thisDay=thisDate.getDate(); var thisMonth=thisDate.getMonth(); var thisYear=thisDate.getFullYear(); var mName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October","November", "December"); var wdName = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); return wdName[thisWDay]+", "+mName[thisMonth]+" "+thisDay+", "+thisYear; } function calcThanksgiving(year) { var delta = year - 1900; var tday = delta % 12; tday = (tday + Math.floor(tday/4) + Math.floor(delta/12)) % 7; return (tday == 0)? 28 : (29 - tday); } function daysToThanksgiving() { var thisDate = new Date(); var year = thisDate.getFullYear(); var tday = calcThanksgiving(year); var tdate = new Date("November "+tday+", "+year); if (thisDate > tdate) { year++; tday = calcThanksgiving(year); tdate = new Date("November "+tday+", "+year); } return Math.floor((tdate-thisDate)/(1000*60*60*24)); } so now i have to call upon this script to print "today is ..the date.. . <br> there are ..days... till thanksgiving im kind of new of calling on javascript files but i came up with Code: <SCRIPT LANGUAGE="JavaScript" SRC="tday.js"> </SCRIPT><script type="text/javascript"> document.write('Today is: '+showDate()+'. There are '+daysToThansgiving()+' days until Thanksgiving.'); </script> and it doesnt seem to be working. Any idea why this doesn't work? I'm trying to hide the li with ID of facebook1 if the innerHTML is one of two things. But if both return false and there is more code in there (e.g. a full url), I want to show the li with ID of facebook1 and also add the text 'Facebook Profile' to the span with ID of fbook-add. Code: <ul> <li id="facebook1"><a href="http://facebook.com"><span id="fbook-add"></span></a></li> </ul> <script type="text/javascript"> if(document.getElementById('facebook1').innerHTML.toLowerCase()=='<a href="http://"><span id="fbook-add"></span></a>') { document.getElementById('facebook1').style.display='none'; } else if(document.getElementById('facebook1').innerHTML.toLowerCase()=='<a href=""><span id="fbook-add"></span></a>') { document.getElementById('facebook1').style.display='none'; } else { document.getElementById('fbook-add').innerHTML=='Facebook Profile'; } </script> I can not figure out why my function is not working or why my alert does not pop up.. can anyone help? When you enter a total in the box, all I need to do is have a total with shipping show up in an alert box. (Minimum shipping is $1.50, if the total is over $25 shipping is 10%) <code> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>CalcShipping</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <script type="text/javascript"> /* <![CDATA[ */ function calcShipping() { var priceInput; var total; priceInput = document.form1.subtotal.value; if (priceInput <= 25) total = 1.5 + number(priceInput); } else { total = number(priceInput) + (number(priceInput) * .1); } window.alert("Your total is $" + total + ); /* ]]> */ </script> </head> <body> <h1>To find out your shipping costs, please enter your purchase price:</h1> <form action ="" name ="form" method = "post" onsubmit="calcShipping();"> <div style="text-align: center"> <input type="text" name="subtotal" value= ""/> <input type="submit" value ="submit"/> </div> </form> </body> </html> </code> Hi All Javascript novice here so just need a bit of help. I'm sure it's something easy I have a form, attributes below: Code: <form id="frmOrder" name="input" method="post" onsubmit="return OnSubmitForm();"> I need the deposit input value to match the total input value if the weeks10 checkbox is ticked. The reason for this is my client requires full payment if this box is ticked. This must be done upon form submission. The reason for this is the email.php and payment.php submission pages collect the deposit value using $_POST and use that as the payment total Attributes for inputs below: Weeks10 checkbox: Code: <input type="checkbox" name="weeks10" id="weeks10" class="weeks10" /> Deposit input: Code: <input type="text" name="deposit" id="txtTotal3" readonly="readonly" size="2" value="0.00"></input> Totals input: Code: <input type="text" readonly="readonly" id="txtTotal" value="0.00" size="2" name="total"/> I'm using the following script: Code: <script type="text/javascript"> function OnSubmitForm() { if(document.input.weeks10[0].checked == true) { document.input.deposit.value = document.input.total.value; } return true; } function OnSubmitForm() { if(document.input.operation[0].checked == true) { document.input.action ="email.php"; } else if(document.input.operation[1].checked == true) { document.input.action ="payment.php"; } return true; } </script> The problem is, the second function works (email.php or payment.php action depending on radio selection). But the first one to adjust the total doesnt Now I know my script is wrong so would somebody be kind enough to lend me a hand? Thanks! *Edit - forgot to mention the example is live here if you need to see the full code. It's a fairly complicated form (for me atleast anyway) http://lake-form.carpbookings.co.uk |