JavaScript - Javascript - Need To Return 2 Argument Values From Function
Ok, guys. I'm new to JavaScript, so go easy. I'll be asking bunches of "dumb" questions for a while.
1st question, and the only one for now ... Say I have the following function: Code: function SwapValues(SwapVal1,SwapVal2) { TempSwapVal=SwapVal1; SwapVal1=SwapVal2; SwapVal2=TempSwapVal; } I don't care about the return value of the function itself, but I do need the new values of SwapVal1 and SwapVal2. In ASP when I do this, the values get returned. How do I get this to work in JS? Similar TutorialsHi, I am facing a problem in passing replace() function as an argument in user defined java function, can any one help me how to resolve it? intention is to pass a file path to my user defined function, but before passing the path i want to replace the character '\' to '\\' I am posting my javascript function he <a href="#" onclick="OpenDocPreview('<%# Eval("PATH")%>'.replace(/\\/g,"\\\\"), '<%# Eval("Filename")%>')"><%# Eval("DocTitle") %></a> function OpenDocPreview(url, docname) { alert('message from search base : ' + url + ' ' + docname); } thank you, Hi guys , i have this function that allows me to hide a <div ID="ID1"> . function hide(X) { document.getElementById('X').style.visibility='hidden'; } <p><a href="#" onclick="hide('ID1');">show me/</a></p> <div ID="ID1" style="visibility:hidden">This is the content</div> the issue the function only works when i change X by ID1. i want to make it for any ID called with the function by assigning any DIV "ID" as an argument. any help ? thanks Hello, I am trying to execute a javascript function passed as argument. I am not sure of the exact way to do it. <html> <head> <script type="text/javascript"> var checkLink = function(linkId, enabledFunction){ var linkidObj = document.getElementById(linkId); if(linkidObj.className=="activelinktext" ) { enabledFunction(); return false; } else if(linkidObj.className=="inactivelinktext") { return false; } alert("*inside CheckLink*"+enabledFunction); }; </script> </head> <body> <a class="inactivelinktext" onclick="checklink(this,"javascript:goback();")"> </body> </html> Thanks, -Vijay Hi there, I am trying to build an interface to upload files and change some of the form (hidden) values depending on the file uploaded, here is where I got so far: Code: $('#buttonUpload').click(function (){ $.ajaxFileUpload({ url:'doajaxfileupload.php', secureuri:false, fileElementId:'fileToUpload', dataType: 'txt', success: function (data, status){ alert("File is uploaded,"); if(typeof(data.error) != 'undefined'){ if(data.error !=''){ alert(data.error); } else{ alert(data.msg); } } document.getElementById('buttonUpload').style.color="green"; document.getElementById('isfileuploaded').value="green"; }, error: function (data, status, e){ alert(e); } }); return false; }); My problem is that I want doajaxfileupload.php to return a variable to javascript/change some html form value. The code works as it is for the file upload, I just need to add something for the output, any suggestion? Edit: I just noticed that some previous part of the code is still the the part that changes the color of the button depending if the file is uploaded or not. Now I want those values to change depending on what file is uploaded. Hi, I've a Javascript function where i'm sending an return value. How do i capture this return value from HTML. My javascript func is as below: Code: function check() { var frm=document.forms.stream_selection sel_num = new Array(); Parr = new Array(); for(j=1;j<1000;j++) { if(frm[j] != null) { if(!frm[j].checked) { frm[0].checked=false } else { Parr.push(j); ParrStr=Parr.join(','); alert(Parr); } } } return(ParrStr); } My HTML call function is as below: Code: echo "<td><center><input name=\"sstreamname\" type=\"checkbox\" onclick=\"check():return true;\"></center></td></tr></font></center></tr></font></center>"; How do i get the value of ParrStr and transfer this to next form. I'm using PHP . Thanks, Ramki. I want to pass dynamic array values to java script function.how its possible. explain with some code.........
I'm trying to "progressively enhance" one of my surveys using javascript. Basically, I have rating scales that make use of radio buttons as each point on the scale. Each radio button occupies its own cell in a table. I wrote some functions that will highlight cells on mouseover in a color corresponding to its position on the scale (e.g. the lowest point is red, the midpoint is yellow, the highest point is green). When a radio button is clicked, the background of the button's cell and preceding cells in the same row will be colored accordingly. The functions are working well in FireFox and Chrome (I just have to add a few lines using the addEvent function to make it compatible with IE). The effect looks a lot nicer when I add a function that makes the visibility of the radio buttons hidden. However, I want to make sure that there is a fallback option in case the functions that color the cells don't work for whatever reason. I would not want the radio buttons hidden in this case. Is there a method whereby I can call the "hideRadiobuttons" function only if the other functions are successfully executed? I have a simple function to display an image when called, but if I try to rewrite the function to take the image as an argument, it stops working. I've looked at other function examples on the web but can't figure out what I am missing. Can anyone help? works: Code: <html> <script language="JavaScript"> var ImagePlusSign = '<img src="plus.jpg" name="slide" width="65" height="50"/>'; function FlashImagesThenWait() { document.getElementById("first").innerHTML = ImagePlusSign } setTimeout(FlashImagesThenWait, 1500); </script> <body>Hello. <div id="first"> </div> </body> </html> does NOT work : Code: <html> <script language="JavaScript"> var ImagePlusSign = '<img src="plus.jpg" name="slide" width="65" height="50"/>'; function FlashImagesThenWait(z) { document.getElementById("first").innerHTML = z } setTimeout(FlashImagesThenWait(ImagePlusSign), 1500); </script> <body>Hello. <div id="first"> </div> </body> </html> Hi, New to JS. Have ordered some new books, but need to get somethings going in the mean time. What I wanted to do is to link to a new page having a date range input in the form of 6 text boxes, 2 sets of mm-dd-yy, from and to, where the upon loading the page box 1 of the from would auto focus and then auto tab and then post where php could take over on the server side. Setting up the form and the lay out, no problem. Auto focusing went just like expected using the following code, included because on the next step everything fell apart. [/CODE] <script type="text/javascript" > window.onload=function() { document.getElementById("input1").focus(); } </script> [/CODE] So then the wheels came off and in response, began to simplify what was trying to be done to find where the issues were. As far as I can get working is: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <script type="text/javascript" > window.onload=function() { document.getElementById("input1").focus(); } </script> <script type="text/javascript" > function myFunc() { alert("is this working"); } </script> </head> <body> <input id="input1" name="input1" type="text" onkeyup="myFunc()" maxlength="2" value="type a No." /> </body> </html> First issue with this: When I first get to the page everything is just as ordered. The focus is on the text box and the default value is "type a No." When I press a key onkeyup, I bring up the alert box with "is this working" in it. But when I refresh the page with the refresh button, after clicking the "ok" on the alert box, the character that was typed in is still displayed. If I use the link to the page, or the URL from the address bar, then the page reloads properly with the default value, "type a No". Question 1: How do you get the default textbox value using the refresh button as apposed to reloading the page using a link or from the address bar? Using Firefox 8.0 Second and more important at the moment: If I change the script to pass an argument to the function the script crashes. It does not pull up an alert box and freezes. My guess is that I'm doing something wrong on a concept level. NOTE: changed the default value of the text box to " ". Looked like to me there was an issue with the size specification and the default string length as you could only enter in a key stroke by highlighting the text and replacing it Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <script type="text/javascript" > window.onload=function() { document.getElementById("input1").focus(); } </script> <script type="text/javascript" > function myFunc(awe) { alert(awe); } </script> </head> <body> <input id="input1" name="input1" type="text" onkeyup="myFunc("123")" maxlength="2" value="" /> </body> </html> Question 2: can someone straighten me out with this? This has me completely stumped. I have a multiple select form element in my HTML document that needs to be manipulated by two different sets of context-sensitive controls. One set of controls is marked up as follows: Code: <div id="divControls1" name="divControls1" style="display:none"> <form id="fControls1" name="fControls1"> <input type="button" id="btnAdd" name="btnAdd" value="Add" onClick="addStuff();" /> <input type="button" id="btnEdit" name="btnEdit" value="Edit" onClick="editStuff();" disabled /> <input type="button" id="btnDelete" name="btnDelete" value="Delete" onClick="deleteStuff();" disabled /> </form> </div> When I load this page into Safari (on Mac OS X) and set the style of divControls1 to "display:block," I have an enabled "Add" button, a disabled "Edit" button, and a disabled "Delete" button, just as I expected. (I monitor selections in a multiple selection element to turn the buttons on and off.) But when I load this page into Firefox (also Mac OS X), all three buttons are disabled at startup. My page runs a function called startup() when the body fires onLoad. To try to troubleshoot the problem, I wrote this line at the beginning of the startup() function: Code: function startup() { alert (document.getElementById("btnAdd").disabled); ... When I run this code in Safari, the alert returns "false" (not disabled), just as I would expect, and intended. However, the same code in Firefox (Mac OS X) returns "true" (disabled) ... but the same code in Firefox (WinXP) returns "false"! Any insight into what might be happening here? 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> Jackhammer </title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <script type="text/javascript"> /* <![CDATA[ */ var jackhammers = new Array(11); var curJackhammer = 0; var direction; var begin; jackhammers[0] = "jackhammer0.gif"; jackhammers[1] = "jackhammer1.gif"; jackhammers[2] = "jackhammer2.gif"; jackhammers[3] = "jackhammer3.gif"; jackhammers[4] = "jackhammer4.gif"; jackhammers[5] = "jackhammer5.gif"; jackhammers[6] = "jackhammer6.gif"; jackhammers[7] = "jackhammer7.gif"; jackhammers[8] = "jackhammer8.gif"; jackhammers[9] = "jackhammer9.gif"; jackhammers[10] = "jackhammer10.gif"; function bounce() { if(curJackhammer == 10) curJackhammer = 0; else ++curJackhammer; /* Firefox does not register an error with the line of code below, but other browsers do . . . It looks like it is a typo left over from the slightly more advanced technique in 10-4 where jackhammers.src is defined. Also, were you to comment out the final line in this function you would see the animation does not work. In browsers other than Firefox which apparently just ignores this code and no animation occurs, the image element is assigned the source value of "undefined" and of course no image is named "undefined". */ document.getElementsByTagName('img')[0].src = jackhammers[curJackhammer].src; if(curJackhammer == 0) direction = "up"; else if(curJackhammer == 10) direction = "down"; document.getElementsByTagName('img')[0].src = jackhammers[curJackhammer]; } function startBouncing() { if(begin) clearInterval(begin); begin = setInterval("bounce()",90); } /* ]]> */ </script> </head> <body> <h1> Jackhammer Man </h1> <p> <img src="jackhammer0.gif" height="113" width="100" alt="Image of a man with a jackhammer." /> </p> <form action="" enctype="text/plain"> <p> <input type="button" value="Start Bouncing" onclick="startBouncing();" /> <input type="button" value="Stop Bouncing" onclick="clearInterval(begin);" /> </p> </form> </body> </html> My teacher insists that the above code is valid (straight out of our book). He says this because (I'm guessing) it runs okay in Firefox and other browsers. It also throws a ton of error messages in Chrome and makes no logical sense. We define the values of the jackhammer array as literal strings, and then the book instructs us to change the source of the image element on the page to jackhammers[i].src. It then has us change the source of the image element again, correctly at the end of the function. It also has us create a useless variable: "direction" (and I say useless because we never use it, it is just fluff I guess). On a side note, would some of you guys like to check out this animation I created here and let me know what you think. Thanks guys. hi,i don't understand about the jquery basic example the code is below PHP Code: <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $('p').addClass(function(n){ return 'par_' + n; }); }); }); </script> <style type="text/css"> .par_0 { color:blue; } .par_1 { color:red; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>Add classes to p elements</button> </body> </html> i want to understand about the how is work function(n){ in the function a and how work return function return 'par_' + n; thanks mate My function the_magnitude() takes in values from 4 different asp:textboxes. The textboxes "easting" and "northing" contain numbers that don't change, but the textboxes "east" and "north" contain numbers inputed by the user, and the function returns "error" as a value to be put into another textbox that we'll call "error box". So what it's suppose to do is when I update the information in either "east" or"north" it will update, on change, the number in the "error box". It kinda works. My problem is that if the number is a value that changes the style to red, #ff0000, in the function it wont update the "error box" and it just leaves a red zero that is a default number in the box. The only way I can get the number to change is if I end up with a number less than 200, which would make the text green. I'm pretty sure it's a problem with my javascript. here is my function Code: <script type="text/javascript"> function the_magnitude(easting, east, northing, north, error) { var a = easting.value; var b = east.value; var c = northing.value; var d = north.value; var total = Math.sqrt(((a - b) * (a - b)) + ((c - d) * (c - d))); if (total > 200) { document.getElementById('TextBox155').style.color = "#ff0000"; document.getElementById('TextBox155').style.fontWeight = "bold"; } else { document.getElementById('TextBox155').style.color = "#008000"; document.getElementById('TextBox155').style.fontWeight = "bold"; error.value = roundNumber(total); } } </script> and here are the two buttons that I input numbers into for east and north Code: <html> <body> <asp:TextBox ID="TextBox153" runat="server" Height="0.25in" Style="font-family: Arial, Helvetica, sans-serif; font-size: small; text-align: center" Width="0.75in" onchange="the_magnitude(document.getElementById('TextBox153'), document.getElementById('TextBox212'), document.getElementById('TextBox154'), document.getElementById('TextBox213'), document.getElementById('TextBox155'))" ></asp:TextBox> </body> </html> Code: <html> <body> <asp:TextBox ID="TextBox153" runat="server" Height="0.25in" Style="font-family: Arial, Helvetica, sans-serif; font-size: small; text-align: center" Width="0.75in" onchange="the_magnitude(document.getElementById('TextBox153'), document.getElementById('TextBox212'), document.getElementById('TextBox154'), document.getElementById('TextBox213'), document.getElementById('TextBox155'))" ></asp:TextBox> </body> </html> and here is the textbox that I want the return of the function to go into. Code: <html> <body> <asp:TextBox ID="TextBox155" runat="server" Height="0.25in" Style="font-family: Arial, Helvetica, sans-serif; font-size: small; text-align: center" Width="0.5in">0</asp:TextBox> </body> </html> Please help, I have been looking at this all day and I know there must be a simple fix! How do I pass results back to textService so that I can make a call such as textResult = textService(text to pass in); I don't want to use a global variable if I can avoid it. This is the code Code: function textService(text){ req.open("GET", "http://....?text="+text, true); req.onload = showResults; req.send(null); } function showResults() { results = req.responseXML.getElementsByTagName("Result"); } Thank you in advance I'm looking for some assistance. I'm trying to write a javascript to validate a form for a class. When I try to submit the form with none of the fields filled in , it goes to the intercept page without showing any errors. In addition to this, I have an error message. Return is outside of function. I have NO clue what they mean here, or what I've done wrong. Could someone take a look and give me an opinion? Enclosed is the entire javascript and the top portion of the related form. <!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 type="text/javascript"> <!-- Hides script from older browsers --> "function validate()" { if(document.form2.size.value=="Please Choose") { alert("Did you forget to select a size?"); return false; } if(document.form2.quantity.value=="Please Choose") { alert("How many did you want??"); return false; } if(document.form2.email.value=="") { alert("Please enter your Email Address.."); return false; } if(document.form2.verify.value=="") { alert("Could you enter it again please.."); return false; } if(document.form2.verify.value!=document.form2.email.value) { alert("The Email Addresses do not match"); return false; } if(document.form2.email.value.match((/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)) { alert("Email address was not valid please RETRY"); return false; } else return true; } //--> </script> <form id="form2" name="form2" method="post" onSubmit="return validate(this)" action="2intercept.asp"> <table width="150" border="0" cellpadding="0"> <tr> <td><div align="right"> <label for="size">Size Desired:</label> </div></td> <td><div align="left"> <select name="size" id="size"> <option selected="selected">Please Choose</option> <option value="4x6">4x6</option> <option value="5x7">5x7</option> <option value="8x10">8x10</option> </select> </div></td> </tr> <tr> <td><div align="right"></div> <label for="quantity">Quantity Desired</label></td> <td><div align="left"> <select name="quantity" id="quantity"> <option>Please Choose</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5+">5+</option> </select> </div></td> ANY advice, corrections or suggestions would be GREATLY appreciated.. I'm at my wits end with this stuff. Yesterday I started looking into clientside database handling with html5 and localstorage. I've been trying for hours to make a function like the one below, but I can't figure out how to return the name variable. Any ideas? Code: function hent_kontakt_navn(var1, var2) { var db = MYDB; db.transaction(function (tx) { tx.executeSql('SELECT * FROM db_kontakter WHERE field1=\'' + var1 + '\' AND field2=\'' + var2 + '\' LIMIT 1', [], function (tx, results) { var len = results.rows.length, i; for (i = 0; i < len; i++){ var fornavn = results.rows.item(i).fornavn; var etternavn = results.rows.item(i).etternavn; var navn = fornavn + ' ' + etternavn; alert(navn); //This works and alerts the correct value } }, null); }); return navn; //But this one returns undefined. I know variables don't pass like this between functions, but how can I make it work so that the function returns this var? } Hi there, I am trying to pick the larger of 5 numbers and sometimes there is a tie. I would like to prioritize variable c1 if there is a tie. This data is from a quiz I am writing. var c1 = 2; // Takes priority if there is a tie. var c2 = 1; var c3 = 2; var c4 = 0; var c5 = 0; I am trying to figure out that if the function "xmlhttp.onreadystatechange=function()" returns true the whole function "function checkUser(str)" should return true as well....so not sure how to do it. i have banged my head since long so please advise! Here is the code below: -------------------------------------------------------- function checkUser(str) { var sp = document.getElementById("msgs"); if (str=="") { document.getElementById("msgs").innerHTML="Username cannot be empty."; return false; } if (str.length <= 6) { document.getElementById("msgs").innerHTML="Username cannot be less than 6 characters and must not start with a number or a special value"; return false; } if (window.XMLHttpRequest) { //code for IE7+, firefox, chrome, opera, safari xmlhttp=new XMLHttpRequest(); } else { //code for old IE xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { if (xmlhttp.responseText === 'FOUND') { sp.style.color = "red"; sp.innerHTML = "[ERROR]: The username \"" + str + "\" already exist. Please try a different name."; return false; } else if (xmlhttp.responseText === 'NOT FOUND') { sp.style.color = "blue"; sp.innerHTML = "Username: VALID"; return true; } } } xmlhttp.open("GET", "checkUser.php?q="+str,true); xmlhttp.send(); } |