JavaScript - Help, Should Be Easy
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> Similar TutorialsShort 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! 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! 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 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. 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 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 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 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> 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? 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> 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. Hi im trying to make a program that passes an array to a method. the method then finds the smallest number in the array and passes that number back to the main where its printed out. I am getting an error saying: "error: number cannot be resolved to a variable". I am using drjava. here is my code. Code: import java.util.*; public class homeWorkTwo{ public static void main(String[] args) { int[] arrayA; arrayA = new int[10]; arrayA[0]=7; arrayA[1]=5; arrayA[2]=8; arrayA[3]=9; arrayA[4]=2; arrayA[5]=10; arrayA[6]=11; arrayA[7]=1; int lowestNumber = find_sum(number); System.out.print("The smallest number is: "+lowestNumber); } public static int find_sum(int [ ] arrayB){ int isItSmaller=0; int small=0; System.out.println("poop"); for(int i=0;i<=7;i++){ isItSmaller=arrayB[i]; if(small<isItSmaller){} else small=isItSmaller; } System.out.print(small); return small; } } 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 The following quiz template only supports 4 possible answers. Id like to add a 5th answer. Should be quite easy to do, but I dont know any programming. http://javascript.about.com/library/blquizf.htm On the template above each possible answer can be given a different score. Then at the and it adds up the score and displays a message according to your total score. Thanks in advance Heres the code: The following is contained in file quizhead.js: var quiz = new Array(); var r = 0; var analPage = 'results.html'; quiz[r++] =('38901~16~What score would you give to your supervisor?~Bad~Average~Good~Excellent~1~4'); The following goes to on the page where the questions appear on the head section: <script src="quizhead.js" type="text/javascript"> </script> The following is contained in quizbody.js file: // Analysis Quiz Javascript // copyright 20th December 2004 - 13th May 2005, by Stephen Chapman // permission to use this Javascript on your web page is granted // provided that all of the code in this script (including these // comments) is used without any alteration function displayResult(cor) { document.write('<div style="font-size:14px;"><b>You have completed the quiz.<\/b><\/div><blockquote><span style="font-size:12px;">*<br \/>*<br \/>*<br \/>*<br \/><\/span><\/blockquote><div><a href="'+analPage+'?cor='+cor+'">Your result</a><\/div>'); } var qsParm = new Array(); function qs() { var query = window.location.search.substring(1); var parms = query.split('&'); for (var i=0; i<parms.length; i++) { var pos = parms[i].indexOf('='); if (pos > 0) { var key = parms[i].substring(0,pos); var val = parms[i].substring(pos+1); qsParm[key] = val; } } } qsParm['questnum'] = 0; qsParm['cor'] = 0; qs(); var questnum = qsParm['questnum']; var cor = qsParm['cor'];cor=cor%475; function checkAnswer(e,b,g){ var a = -1; var x = Math.floor(g/b); // x = bdca for (var i=0; i<e.c.length; i++) {if (e.c[i].checked) {a = i;}} if (a == -1) { alert("You must select an answer"); return false; } var b = new Array(); b[1] = Math.floor(x/1000); x -= b[1]*1000; b[3] = Math.floor(x/100); x -= b[3]*100; b[2] = Math.floor(x/10); b[0] = x - b[2]*10; cor += b[a]; var www = self.location.href.lastIndexOf('?'); var thispage = self.location.href; if (www != -1) thispage = self.location.href.substr(0,www); questnum++; var p = Math.floor((Math.random() * 8) + 2); var m = (p * 475) +cor; top.location = thispage + '?questnum='+ questnum +'&cor='+m; return false; } function result(cor) { var lo = 0, hi = 0; for (var i=0;i < quiz.length;i++) { var f = quiz[i].split('~'); lo += parseInt(f[7]); hi += parseInt(f[8]); } return Math.round((cor - lo) * 100 / (hi - lo)); } var tblsz = quiz.length; document.write('<table align="center" cellpadding="3" width="350" border="1"><tr>'); if (questnum < quiz.length) { var f = quiz[questnum].split('~'); document.write('<td align="left"><form name="myForm"><div style="font-size:14px;">Question: '+f[2]+'</div><blockquote><span style="font-size:12px;">\n'); document.write('<input type="radio" name="c" value="0" /> '+f[3]+'<br />\n'); document.write('<input type="radio" name="c" value="1" /> '+f[4]+'<br />\n'); if (f[5] != '') document.write('<input type="radio" name="c" value="2" /> '+f[5]+'<br />\n'); if (f[6] != '') document.write('<input type="radio" name="c" value="3" /> '+f[6]+'<\/span><\/blockquote>\n'); document.write('<div align="right"><input type="button" value="Next Question" onclick="checkAnswer(myForm,'+f[1]+','+f[0]+');return false;" /><\/div><\/form>\n'); } else { document.write('<td align="center">\n'); document.write('<form name="myForm">'); displayResult(result(cor)); document.write('<\/form>\n');} document.write('<\/td><\/tr><\/table>\n'); The following goes on the page where the questions appear on the body section: <script src="quizbody.js" type="text/javascript"> </script><noscript><table align="center" cellpadding="3" width="350" border="1"><tr> <td align="left"><div><b>This Quiz requires Javascript</b> </div><blockquote>You either have Javascript disabled <br />or the browser you are using does not<br /> support Javascript. Please use a Javascript<br /> enabled browser to access this quiz.<br /> <br /> </blockquote></td></tr></table></noscript> The following is contained in the quizresh.js file: // Analysis Quiz Javascript // copyright 20th December 2004, by Stephen Chapman // permission to use this Javascript on your web page is granted // provided that all of the code in this script (including these // comments) is used without any alteration var qsParm = new Array(); function qs() { var query = window.location.search.substring(1); var parms = query.split('&'); for (var i=0; i<parms.length; i++) { var pos = parms[i].indexOf('='); if (pos > 0) { var key = parms[i].substring(0,pos); var val = parms[i].substring(pos+1); qsParm[key] = val; } } } qsParm['cor'] = 0; qs(); var cor = qsParm['cor']; This goes on the page where the results are displayed on the head section: <script src="quizresh.js" type="text/javascript"> </script> The following is contained in the quizresb.js file: // Analysis Quiz Results if (cor <= 25) document.write('<p>Good<\/p>'); else if (cor <= 50) document.write('<p>Very Good<\/p>'); else if (cor <= 75) document.write('<p>Excellent<\/p>'); else document.write('<p>Perfect<\/p>'); This goes on the results page on the body section: <script src="quizresb.js" type="text/javascript"> </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> 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> Hi all, I am trying to add this slider: http://cssglobe.com/lab/easyslider1.5/04.html to my website but due to my inexperience can't seem to get it to work.. please help... Here is my 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> <title>Welcome to Somerset Wedding Wishes</title> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> <script type="text/javascript" src="easyslider1.5/js/jquery.js"></script> <script type="text/javascript" src="easyslider1.5/js/easySlider.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#slider").easySlider({ auto: true, continuous: true }); }); </script> <style type="text/css"> <!-- body { background-color: #000000; background-image: url(bg1.jpg); background-repeat: repeat-x; } #slider{} #slider ul, #slider li{ margin:0; padding:0; list-style:none; } #slider li{ width:696px; height:241px; overflow:hidden; } #prevBtn, #nextBtn{ display:block; width:30px; height:77px; position:absolute; left:-30px; top:71px; } #nextBtn{ left:696px; } #prevBtn a, #nextBtn a{ display:block; width:30px; height:77px; background:url(images/btn_prev.gif) no-repeat 0 0; } #nextBtn a{ background:url(images/btn_next.gif) no-repeat 0 0; } --> </style> </head> <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> <!-- ImageReady Slices (Untitled-1) --> <table width="922" height="864" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01"> <tr> <td> <img src="images/home_01.png" width="922" height="230" alt=""></td> </tr> <tr> <td><div id="slider"><ul> <li><img src="easyslider1.5/images/01.jpg" alt="pic1"></li> <li><img src="easyslider1.5/images/02.jpg" alt="pic2"></li> <li><img src="easyslider1.5/images/03.jpg" alt="pic3"></li> ... </ul> </div> <div align="center"></div></td> </tr> <tr> <td> <img src="images/home_03.png" width="922" height="239" alt=""></td> </tr> <tr> <td> <img src="images/home_04.png" width="922" height="170" alt=""></td> </tr> </table> <!-- End ImageReady Slices --> }); </body> </html> I know it's possible to do this in php, but what would the javascript equivalent be? Code: var scripts = new Array(); scripts[] = "PHP"; scripts[] = "ASP"; scripts[] = "JavaScript"; scripts[] = "HTML"; But this obviously doesn't work. And just to clarify, I mean the ability to add an array whenever I want to in my page instead of having to name the array like... Code: var scripts = new Array(); scripts[0] = "PHP"; scripts[1] = "ASP"; scripts[2] = "JavaScript"; scripts[3] = "HTML"; I'm using it for such as in a form: Code: <input type="text" name="formarray[]"> <input type="text" name="formarray[]"> Thank you in advance. |