JavaScript - Functions Question
Similar Tutorialshi. i know a little php, but new to javascript. much of the syntax is identical. in reading the new rhino book (flanagan 6th), he gives the following example: Code: var square = function(x) { return x*x; }; okay, but then he does this: Code: square(plus1(y)) as an example of "invoking two functions in one expresson". (Prior he gives an example of defining a function with plus1, which is how I normally think of functions.) uh, square is a *variable*, but he is using it as a FUNCTION. Can somebody help me understand how this can be? how it works? I'm having trouble conceptualizng how we use a variable as a function. Hi Everyone I am trying to write some code to create an object in JS that contains the x,y coords for a shape i wish to draw. I create the shape, and then in a draw function I draw the shape to the screen from the object I created erlier. Trouble it is does not seem to work :S have anyone got and ideas? Code: var myShape = new newShape(200,200); function init() { canvas = document.getElementById("canvas"); ctx = canvas.getContext("2d"); return setInterval(draw, 50); } // init function newShape(x,y){ this.un_X=x this.un_Y=y function drawAll() { ctx.beginPath(); ctx.arc(un_X,un_Y,30,0,Math.PI*2,true); ctx.closePath(); ctx.stroke(); } // drawAll } // newShape function draw() { ctx.clearRect(0, 0, WIDTH, HEIGHT); myShape.drawAll(); } // draw init(); Thanks in advance I want to learn javascript and I found codecademy.com which seems like a great way to learn the language. Before the first lesson it says Quote: You should be comfortable with loops, if statements, functions, and objects before attempting this course. Can someone explain what these are or point me in the direction where I can find the answers? I tried searching with google, but I havent found anything that clearly states what exactly the loop, object, etc. is. Hi, I have written a number of functions designed to return frequency data on 1000 randomly chosen numbers using different math functions for the rounding. I would like to include all of these functions within the wrapper of another function so that only one call is needed to get returns from all of the 'inner' functions. However, while each of the functions works in isolation, the moment I wrap them in another function they stop working. The following code is one of the functions 'frequencyWrapperOne' that has been wrapped in the function 'testWrapper'. A call to testWrapper does nothing. Can anyone see what I'm doing wrong? Code: function testWrapper() { function frequencyWrapperOne() { var numberArrayOne = new Array(0,0,0); for (var i = 0; i < 1000; i = i + 1) { var chosenNumber = Math.floor(Math.random() * 3); if (chosenNumber == 0) { numberArrayOne[0] = numberArrayOne[0] + 1; } if (chosenNumber == 1) { numberArrayOne[1] = numberArrayOne[1] + 1; } if (chosenNumber == 2) { numberArrayOne[2] = numberArrayOne[2] + 1; } } return window.alert(numberArrayOne.join(',')); } } testWrapper(); Thanks. Ok here is what I have so far, my ending part of my Call Function I think is ok. for my main function. I think I misplaced the { and } I will show you all the codes I have for my main function this is what I have I think I did misplace something but I can not pin point where that one small things should be Code: unction showResults(race,name,party,votes) { // script element to display the results of a particular race var totalV = totalVotes(votes); document.write("<h2>" + race + "</h2>"); document.write("<table cellspacing='0'>"); document.write("<tr>"); document.write("<th>Candidate</th>"); document.write("<th class ='num'>Votes</th>"); document.write("<th class='num'>%</th>"); document.write("</tr>"); } for (var i=0; i < name.length; i++) { document.write("<tr>"); document.write("<td>" name[i] + "(" + party[i] + ")</td>"); document.write("td class='num'>" + votes[i] + "</td>"); var percent=calcPercent(votes[i], totalV) document.write("<td class='num'>(" + percent +"%)</td>"); createBar(party[i],percent) document.write("</tr>"); } document.write("</table>"); } </script> Just wondering if i misplaced any ; or { or } anywhere suggestions? Here is my call function Code: <script type="text/javascript"> showResults(race[0],name1,party1,votes1); showResults(race[1],name2,party2,votes2); showResults(race[2],name3,party3,votes3); showResults(race[3],name4,party4,votes4); showResults(race[4],name5,party5,votes5); </script> I been going over this, I can not seem to figure out what { i might be missing? Thanks Is there a way to activate a function from another function? It has to be in the script tag, it can't be in the HTML section. Can I use something similar to this following code? If not, can anyone give me some help? I have tried to do it various ways, and have looked it up a few times, but to no avail. Can I use something similar to this following code? If not, can anyone give me some help? if (condition) {activate functionname();} Any help I can get would be appreciated. Thanks a lot to anyone who can help. im no jQuery rookie but for the first time ...instead of simply writing out the animation jQuery('element').hide() in that way.... i instead needed to keep track of the functions so i wrote it like function hidetheElement() { jQuery('element').hide() } but now i need to run 2 of those functions together after a click ...for example jQuery('ClickedElement').click( movethelement,hidethelement ) see what i mean?....i dont know how to properly run two defined functions whats the proper syntax.... and here is my actual code for reference (its basically a menu link being clicked...hiding one page...and showing another....i needed to define the functions so if a menu link is clicked it could hide every other page first...by function....and then call a function to show the page Code: jQuery(document).ready( function() { jQuery('#ladiesmenu').click( previewoff,ladieson ); function previewoff() { jQuery('#preview').fadeOut(500); } function ladieson() { jQuery('#ladies').fadeIn(500); } I'm a newbie student coder for now, hoping to enter the wonderful world of software engineering in another year. I put together a very simple site for a class and all is well, but my "Contact SRS" form doesn't seem to want to run my JavaScript code even though I've used a JS debugger which returned no errors. When I click the Submit button it's supposed to validate the form. But I keep getting "Error on page" in the bottom IE 7 message bar (where it usually says "Done" when you load a page). There is another function that also does not work - it's in the Telephone number fields - it' supposed to check that the values entered are numbers. Would anyone be kind enough to try to run this page in your browser and see if the validation functions runs for you? If they don't, could you offer any suggestions as to code errors or possible browser settings that might be causing the problem? Thanks in advance to anyone offering their time!!! 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> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <meta name="description" content="Page used to contact SRS" /> <meta name="keywords" content="resume, resumes, job, jobs, job market, job info, interview tips, job interview, salary, salaries, cover letter, cover letters, SRS, Superior Resume Source, business cards, help with resume, help with job search" /> <title>Contact SRS</title> <link rel="stylesheet" href="style.css" type="text/css" media="screen" /> <script type="text/javascript">/* <![CDATA[ *//* ]]> */ function checkForNumber() { if (isNan(document.forms[0].area.value)){alert("You must enter a numerical value");return false;} else if (isNan(document.forms[0].exchange.value)){alert("You must enter a numerical value");return false;} else if (isNan(document.forms[0].phone.value)){alert("You must enter a numerical value");return false;} } // function validateForm () // { // if(document.forms[0].firstname.value==""){alert("You must enter your first name.");return false;}} // } // else if(document.forms[0].lastname.value== ""){alert("You must enter your last name.");return false;} // else if(document.forms[0].address1.value== ""){alert("You must enter your address.");return false;} // else if(document.forms[0].city.value== ""){alert("You must enter a city.");return false;} // else if(document.forms[0].state.value== ""){alert("You must enter a state.");return false;} // else if(document.forms[0].zip.value== ""){alert("You must enter your zip code.");return false;} // else if(document.forms[0].area.value== ""||document.forms[0].exchange.value== ""||document.forms[0].phone.value== "") // {alert("You must enter a complete phone number.");return false;}} // else {alert('Thank you! SRS will contact you within one business day.');}} function isEmpty(strfirst, strlast, straddress, strcity, strstate, varzip) { strfirst = document.forms[0].firstname.value strlast = document.forms[0].lastname.value straddress = document.forms[0].address1.value strcity = document.forms[0].city.value strstate = document.forms[0].state.value varzip = document.forms[0].zip.value //name field if (strfirst == "" || strlast == null || strfirst.charAt(0) == ' ') { alert("\"First Name\" is a mandatory field.\nPlease amend and retry.") return false; } //last name field if (strlast == "" || strlast == null || strlast.charAt(0) == ' ') { alert("\"Last Name\" is a mandatory field.\nPlease amend and retry.") return false; } //address field if (straddress == "" || straddress == null || straddress.charAt(0) == ' ') { alert("\"Address\" is a mandatory field.\nPlease amend and retry.") return false; } return true; } //city field if (strcity == "" || strcity == null || strcity.charAt(0) == ' ') { alert("\"City\" is a mandatory field.\nPlease amend and retry.") return false; } //state field if (strstate == "" || strstate == null || strstate.charAt(0) == ' ') { alert("\"State\" is a mandatory field.\nPlease amend and retry.") return false; } } function check(form)){ if (isEmpty(form.firstname)){ if (isEmpty(form.lastname)){ if (isEmpty(form.address1)){ if (isEmpty(form.city)){ if (isEmpty(form.state)){ } } } } } return false; } </script> </style> </head> <body> <a name="top"></a> <div id="container"> <div id="header"> <h1></h1> </div> <div id="content"> <h2>Contact SRS</h2> <p>Use this page to contact SRS and/or order products and services. Refer to our <a href="services.htm">Services</a> page for pricing.</p> <p><strong>Remember, our work is guaranteed or your money back!</strong></p> </div> <div id="sidebar"> <ul> <li><a href="index.htm">Home</a></li> <li><a href="about.htm">About SRS</a></li> <li><a href="services.htm">Services</a></li> <li><a href="contact.htm">Contact SRS</a></li> </ul> <div class="widget"> Did you know that employers prefer CV's that are limited to three pages or less? </div> <div class="widget"> First impressions last a lifetime... write a Thank You letter EVERY time! </div> <div class="widget"> Want to know your personality type? Take this FREE <a href="http://www.careerfulfillment.com/?hop=daviduk"> personality test.</a> </div> </div> </div> <form method="get" enctype="application/x-www-form-urlencoded"> <table border="0"> <tr> <td id="contact table" valign="top"> <h3><strong>Contact SRS</strong></h3> <p><strong>* Required</strong><p> <p>First Name*<br /> <input type="text" name="firstname" size="50" /> </p> <p>Last Name*<br /> <input type="text" name="laststname" size="50" /> </p> <p>Address*<br /> <input type="text" name="address1" size="50" /><br /> <input type="text" name="address2" size="50" /> </p> <p>City, State, Zip*<br /> <input type="text" name="city" size="34" /> <input type="text" name="state" size="2" maxlength="2" /> <input type="text" name="zip" size="10" maxlength="10" onchange="return checkForNumber;" /> </p> <p>Telephone*</p> <p>(<input type="text" name="area" size="3" maxlength="3" onchange="return checkForNumber;" />) <input type="text" name="exchange" size="3" maxlength="3" onchange="return checkForNumber;" /> <input type="text" name="phone" size="4" maxlength="4" onchange="return checkForNumber;" /></p> <strong>Check the reason(s) for your inquiry?*</strong> <p><input type="checkbox" name="geninquiry" value="General_Inquiry" />General Inquiry <br/> <input type="checkbox" name="help" value="Help" />Help<br/> <input type="checkbox" name="new_resume" value="Order New Resume" />Order a New Resume <br /> <input type="checkbox" name="Update_resume" value="Update My Resume" />Update My Resume <br/> <input type="checkbox" name="cover_letter" value="Order Cover Letter" />Order a Cover Letter <br /></p> <textarea rows="10" cols="50">Type your comments here</textarea> <p><input type="button" name="Submit" value="Submit" onsubmit="return check(this);" /> <input type="reset" /></p> </td> </tr> </table> </form> <a href="#top">Back to Top</a><br /> </body> </html> . Why do we have functions without parentheses at times? Ex 1) http://www.youtube.com/watch?v=OHYFNDzlDTE (On 2:18) Ex 2) At the bottom: Code: var panel, flag; function mousemoveResponse(e) { var x, y; if( window.event ) { x= event.x; y=event.y; } else if(e) { x=e.pageX; y=e.pageY; } if(flag) { panel.innerHTML = "Mouse is at X: " + x + ", Y: " + y ; } } function mouseoverResponse() { flag = false; panel.innerHTML = "Mouse is Over"; } function mouseoutResponse() { flag = true ; } function init() { panel=document.getElementById("panel"); flag = true; panel.innerHTML="Move the mouse..." ; document.onmousemove=mousemoveResponse; panel.onmouseover=mouseoverResponse; panel.onmouseout=mouseoutResponse; } onload=init; Hi, I am creating a mashUp with google maps and have a function that creates the map, marker, infowindow and all works great. I want to add more functions to that functions file. Do I start with another function Name() put my function in the {} and then I am going to use the onclick event when I want the function to run. I also am going to add more functions to this file Is that the correct way to keep adding functions? And what is the best way of calling them in my html file? Answers and examples would be great thank you I need to input the functions for the buttons ( Push, Pop,Peek, Enqueue, and Dequeue) nothing I try works. Can anyone possible help me with one of the push, pop or peek, and then one of the Enqueue or Dequeue (any information I see online always has to do with wordpress which im not using) . Im really not asking for someone to do all the work for me, I just need a start. ok here is my code with the buttons just no functions <html> <head> <title>COP2500 - Lab 9</title> </style></head> <body> <center> <h3><b>COP2500 Assignment 9</b></h3> <b>Queue:</b> <form name="queue"> <input type=text size=60 name="display"><br> <input type=text size=5 name="entry"> <input type=button value="Enqueue" onClick="enqueue()"> <input type=button value="Dequeue" onClick="dequeue()"> </form> <hr> <b>Stack:</b> <form name="stack"> <table> <tr> <td>Entry: <input type=text size=5 name="entry"><br> <input type=button value="Push" onClick="push()"><br> <input type=button value="Pop" onClick="pop()"> <br> <input type=button value="Peek" onClick="peek()"><br> </td> <td><textarea name=display rows=20></textarea></td> </tr> </table> </form> </center> <script language="JavaScript"> var myqueue = new Array(); var front; var back; front = 0; back = -1; var mystack = new Array(); var top; top = -1; function enqueue() { // This function should take the value from the 'entry' text box and // add it to the back of the queue. The 'back' queue variable must be // incremented. // Since the queue changed, call the display function. Done. display_queue(); } function dequeue() { // This function should display the value of the 'front' element // in the queue using the 'alert' function and then increment the value // of the front variable. // if the queue is empty, i need to say so. // Since the queue changed, call the display function. Done. display_queue(); } function push() { // This function should add the value in the 'entry' text box to the stack, // and then increment the value of the 'top' variable. // Since the stack changed, call the display function. . display_stack(); } function pop() { // This function should use the alert function to display the top // of the stack and then remove the top item from the stack. // You can 'remove' the item simply by decrementing the value of 'top'. // If the stack is empty, it needs to say so. // Since the stack changed, call the display function. . display_stack(); } function peek() { // This function should use the 'alert' function to display the top // of the stack but not change the stack. If stack is empty,it needs to say so. } // ***************************************************************** // Do not edit anything below this line. Use the functions as given. // ***************************************************************** function display_queue() { var output; var i; output = " "; for (i = back; i >= front; i--) output = output + myqueue[i] + " "; document.queue.display.value = output; } function display_stack() { var output; var i; output=""; for (i = top; i > -1; i--) output = output + mystack[i] + "\n"; document.stack.display.value = output; } </script> </body> </html> I would really like to thank the guys that have been helping me. I think my brain would have exploded otherwize. Some of the problems have been simple misspellings, but when you have been staring at the same thing for hours it can sorta slip by. HOPEFULLY this will be the last thing that I need some help with. Bascially Im not sure if Im using the function command properly. When Submit Details is click it should validate the form and then when Calculate is pressed calculate the form (obviously). I think maybe my var Array table is wrong? And the way the functions are set out doesnt seem it sit right. Code: <script type="text/javascript"> var element; var itemArray = new Array(); itemArray [0] = "Bike" //not sure if these are properly writen?// itemArray [1] = "TV" itemArray [2] = "iPod" itemArray [3] = "Car" var flag="OK"; var count = 0 document.write ("<form name='myform'><br><table>"); while (count <= 3) { if (count < 3) { document.write ("<tr><td><value>Enter money from " + itemArray [count] + " sale.</td><td><input type='text' name=" + itemArray [count] + "></td></tr>"); count++ } else { document.write ("<tr><td><value>Enter the price of " + itemArray [count] + ".</td><td><input type='text' name=" + itemArray [count] + "></td></tr>"); document.write ("<tr><td><input type='submit' value='Submit Details' onClick='validateform'</td></tr>"); document.write ("<tr><td><input type='submit' value='Calculate Details' onClick='calculateform'</td></tr></table></form>"); count++ } } function validateform() //<=== doesnt run { element=document.getElementsByTagName ('input'); for (counter=0; counter<element.length; counter++) { switch (element[counter].type) { case "submit": break; default: if (isNaN(element[counter].value) || (element[counter].value == "") ) { alert("You need to enter a number into " + element[counter].name); flag="NotOK" } else { Bike=element[0].value; TV=element[1].value; iPod=element[2].value; Car=element[3].value; } break; } } } function calculateform() //<=== Doesnt run either { if (flag=="OK") { if ( (Number(element[Bike])) && (Number(element[TV])) && (Number(element[iPod])) && (Number(element[Car])) ) //<========= Gets Bike is not defined error easy// { TotalMoney= parseFloat (Bike) + parseFloat (TV) + parseFloat (iPod) if (TotalMoney >= Car) { alert ("The total money is " + TotalMoney + " and the car price is " + Car + " and you can afford the car") } else { alert ("The total money is " + TotalMoney + " and the car price is " + Car + " and you cannot afford the car") } } } } </script> Code: <html> <head> <script> //declare variable //var temp = 0; var Num = parseInt(prompt('Enter an Intger')); // starts the function factorial factorial(Num); //The function is declared function factorial(currentNumber) { if(currentNumber > 1) { //temp = temp + (currentNumber); factorial(currentNumber - 1); alert(currentNumber); } else { alert("here"); } } </script> </head> <body> </body> </html> This code is suppose to take the prompt number and keep subtracting one if > 1 but it seems to be not doing that at all and doing the else statement first then + till it gets to the prompt number. I have been staring at it for a while now and cant work it out. Thanks Hi all, I am trying to pass some dynamic values for use with javascript or more specifically AJAX... I had a set up looking something like the following; Code: <input type="button" onclick="ajaxFunction()" value="Add Favourite"/> <input type="text" name="school_id" id="school_id" value="<?php echo $school_id; ?>" /> <input type="text" name="teacher_id" id="teacher_id" value="<?php echo $teacher_id; ?>" /> Code: <script language ="javascript" type="text/javascript"> <!-- //Browser Support Code function ajaxFunction(){ var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); }catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); }catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data // sent from the server and will update // div section in the same page. ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ var ajaxDisplay = document.getElementById('ajaxDiv'); ajaxDisplay.value = ajaxRequest.responseText; } } // Now get the value from user and pass it to // server script. var school_id = document.getElementById('school_id').value; var teacher_id = document.getElementById('teacher_id').value; var queryString = "?sid=" + school_id; queryString += "&tid=" + teacher_id; alert(queryString); ajaxRequest.open("GET", "ajax_insert_favourite.php" + queryString, true); ajaxRequest.send(null); } //--> </script> however because it is possible for there to be more than one button, i.e. many teachers can be returned the javascript would also output 1 and 1. I need someway of making this unique to the id's... I think that passing arguments/params might be the solution but I'm having a hard time implementing it; I've tried the following; Code: <input type="button" onclick="ajaxFunction('school_id','teacher_id')" value="Add Favourite"/> Code: <!-- //Browser Support Code function ajaxFunction(school_id,teacher_id){ But once I do this, it no longer even make the call. Can anyone advise here, I'm a relative newbie to javascript/AJAX etc. Many thanks, Greens85 Hello, can you help me work it out - I need a link to do two things at the same time - the main function of opening url and executing javascript. How can i do that?
I'm trying to unravel / understand / re-code a website i've inherited. I can just about work my way around the html, but the javascript is proving to be a bit more taxing! Anyway - the pages all have the following javascript functions coded into the head. I'm assuming a function is only activated if it's referenced from the body. I can't find any reference to these functions in the body and... if i remove them the pages seem to work ok - but of course they may be doing something subtle in the background that i'm unaware of so... Could someone give me an idea as to what these functions are likely to be doing, and if they are actually useful/needed? Code: function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} } function MM_swapImgRestore() { //v3.0 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc; } function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_swapImage() { //v3.0 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} } Hello all, I’ve got a homework to do and I'm stuck, as you can see i have done all the function's (i think they are ok but not shure) with a main program I'm stuck don’t know even where to begin :/ could anyone please please help a bit? best regards <HTML> <HEAD> <TITLE> The game of 21 </TITLE> <SCRIPT LANGUAGE = "JavaScript"> //FUNCTIONS /* *removes a given number of sticks * *function takes one argument numberToRemove that represents the number of sticks to be removed * if numberToRemove is more than numberOfSticks, reduces numberOfSticks to 0 * otherwise reduces numberOfSticks by numberToRemove *function returns no value */ function removeSticks(numberToRemove) { if (numberOfSticks< numberToRemove) { numberOfSticks =0 } else { (numberOfSticks = numberOfSticks - numberToRemove); } } /* *switches current player * *function takes no argument * if current player is 1, sets current player to 2 * otherwise sets current player to 1 *function returns no value */ function switchCurrentPlayer() { if (currentPlayer ==1) { return currentPlayer = 2; } { return currentPlayer = 1; } } /* *gets a number from a prompt dialogue * *function takes no argument * prompts the current player for a number of sticks to remove, with a suitable message *function returns the number the player enters */ function getChosenNumber() { ChosenNumber = window.prompt('Player ' + currentPlayer + ': Please enter either 1, 2 OR 3.'); return ChosenNumber; } //VARIABLES -- do not change this section var numberOfSticks = 21;//number of matchsticks, initially 21 var currentPlayer = 1;//player whose turn it is, Player 1 goes first var numberChosen;//variable used to hold the number the current player chooses //MAIN PROGRAM The players take turns to choose how many matches to remove, as long as there are still matches left. After each go the program outputs the number of matches that still remain and if this number is zero a message is displayed to say the current player is the winner. The turn then passes to the other player. </SCRIPT> </HEAD> <BODY> </BODY> </HTML> I'm sorry if this is asking too much I just find it much easier to learn interacting directly with experience coders but I'm tryign to understand functions better and wanna make sure I got it right. the function syntax looks like this right Code: function functionname() { function code } the perameters kinda confuses me, inside them can go variables right? but where do you declare those variables? before the function or in the function? is it like this? Code: function functionname(x) { var x="Hello" } if thats right do you write the code you want to execute below the variables? like under var x I wanna make a alert I would write alert("x") Below are 2 JS functions I currently use on a site but as it is if the checkbox (1st function) is not ticked & the submit button is clicked the alert box shows but the progress image (2nd function) also shows, I was wondering if it is possible to merge these 2 together so that the progress image will only show if all criterias are filled ie the checkbox is ticked... and if so how it can be done or should I say can you post the code as I have no idea about JS these are just 2 snippets I found Code: function validate(form) { if(!document.theform.terms.checked){alert("Please read our terms and check the box"); return false; } return true; } Code: function loadSubmit() { var ProgressImage = document.getElementById('progress_image'); document.getElementById("progress").style.visibility = "visible"; setTimeout(function(){ProgressImage.src = ProgressImage.src},100); return true; } If it helps or relevant ... The form: Code: <form action="xxx.php" method="post" name="theform" onsubmit="return validate(this)"> The progress bar/image: Code: <input name="submit" type="submit" value="submit" class="contSubmit" id="btn" onclick="return loadSubmit()"> <p style="visibility:hidden;" id="progress"/><img id="progress_image" src="images/image.gif" alt=""><br />In Progress ...<p> Thank you in advance for any help |