JavaScript - "this" Keyword Not Working
When this page loads the table should fill using onload="fillTable()" with 4 rows from an array called foodArray. Each row gets a grab button assigned with a unique id. Then when you click on one of the buttons, it should call function addRow. In addRow, i'm trying to use the this keyword to refer to the button that called it, then use an alert window to show the button id. but when the script runs, alert is not giving the id its just a blank alert window. whats wrong with it?
Thanks, Geoff 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/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> </style> <link href="basicstyle.css" rel="stylesheet" type="text/css" /> <link href="tablestyle.css" rel="stylesheet" type="text/css" /> <link href="advancestyle.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> var food1 = new Array("-", "Boneless Chicken Breast", "M&M", "Meat & Alternatives", "1 breast", "125g", 110, 2, 0.5, 0, 65, 650, 0, 0, 0, 22); var food2 = new Array("-", "Teriyaki Style Rice", "Uncle Ben's Fast & Fancy", "Grain Products", "1/4 package", "44g", 160, 0.5, 0.2, 0.1, 0, 520, 34, 0, 4, 3); var food3 = new Array("-", "Whole Kernel Corn", "Irrestibles", "Fruit & Vegetables", "1/2 cup", "125mL", 80, 0.5, 0, 0, 0, 10, 15, 2, 1, 2); var food4 = new Array("-", "Ketchup", "Heinz", "Condiments", "1 tbsp", "15mL", 20, 0, 0, 0, 0, 140, 5, 0, 4, 0.3); var foodArray = [food1, food2, food3, food4]; var buttonValue = "Grab"; var onclickString = "addRow()"; //**************************************same fillTable as in aisle1************************************ function fillTable( arr, p ) { //fill the output table with whatever array is passed to it in form of arr parameter. outputTableRows = 5; outputTableColumns = 16; for (var x = 0; x < outputTableRows; x++) { var xPage = (x + 5 * (p - 1)); for (var y = 0; y < outputTableColumns; y++) { //set id variable in order to refer to each cell in the output table by this id. var id = x + "_" + y; if ( (arr.length > xPage) && (arr[xPage][y] != undefined) ) { //if its the first column, put in a button, or else put in the resultArray[x][y] value in the output table cell. if (y == 0) document.getElementById(id).innerHTML = "<input id=\"b" + id + "\" type=\"button\" value=\"" + buttonValue + "\" onclick=\"" + onclickString + "\">"; else document.getElementById(id).innerHTML = arr[xPage][y]; //else if the resultArray entry is undedefined, just put in a dash in the output table cell. } else { document.getElementById(id).innerHTML = "-"; } } } } function addRow ( e ) { if (!e) var e = window.event; alert ("Add row"); alert (this.id); var insertFoodRow = ""; var t2body = document.getElementById("t2body"); var row = document.createElement("tr"); } </script> </head> <body onload="fillTable( foodArray, 1 )"> <div id="dcontainer" class="type3div"> <div id="dheader" class="type2div"> <div id="dlogo" class="type1div"></div> <div id="dlogin" class="type1div"></div> </div> <div id="dmenubar" class="type2div"> <a href=""><div id="dmenu1" class="navigation">Page 1</div></a> <a href=""><div id="dmenu2" class="navigation">Page 2</div></a> <a href=""><div id="dmenu3" class="navigation">Page 3</div></a> <a href=""><div id="dmenu4" class="navigation">Page 4</div></a> <a href=""><div id="dmenu5" class="navigation">Page 5</div></a> </div> <div id="dcontent" class="type2div"> <h1>Grocery Store - Aisle 3:</h1> <h2>Make a combo or recipe</h2> <div id="content1" class="type1div"> <div id="table2" class="outputTable"> <table border="1"> <thead> <tr> <th class="tableHeadMediumCenter">Edit</th> <th class="tableHeadLargeLeft">Food</th> <th class="tableHeadLargeLeft">Brand</th> <th class="tableHeadLargeLeft">Food Type</th> <th class="tableHeadLargeLeft">Serving Amount</th> <th class="tableHeadSmallCenter">SS</th> <th class="tableHeadSmallCenter">Cal</th> <th class="tableHeadSmallCenter">Fat</th> <th class="tableHeadSmallCenter">Sat</th> <th class="tableHeadSmallCenter">Trans</th> <th class="tableHeadSmallCenter">Chol</th> <th class="tableHeadSmallCenter">Sod</th> <th class="tableHeadSmallCenter">Carb</th> <th class="tableHeadSmallCenter">Fib</th> <th class="tableHeadSmallCenter">Sug</th> <th class="tableHeadSmallCenter">Pro</th> </tr> </thead> <tbody id="t2body"> </tbody> </table> </div> </div> <br /> <div id="content2" class="type1div"> <div id="table1" class="outputTable"> <table border="1"> <thead> <tr> <th class="tableHeadMediumCenter">Edit</th> <th class="tableHeadLargeLeft">Food</th> <th class="tableHeadLargeLeft">Brand</th> <th class="tableHeadLargeLeft">Food Type</th> <th class="tableHeadLargeLeft">Serving Amount</th> <th class="tableHeadSmallCenter">SS</th> <th class="tableHeadSmallCenter">Cal</th> <th class="tableHeadSmallCenter">Fat</th> <th class="tableHeadSmallCenter">Sat</th> <th class="tableHeadSmallCenter">Trans</th> <th class="tableHeadSmallCenter">Chol</th> <th class="tableHeadSmallCenter">Sod</th> <th class="tableHeadSmallCenter">Carb</th> <th class="tableHeadSmallCenter">Fib</th> <th class="tableHeadSmallCenter">Sug</th> <th class="tableHeadSmallCenter">Pro</th> </tr> </thead> <tbody> <tr> <td id="0_0" class="tableCellCenterTop">-</td> <td id="0_1" class="tableCellLeftTop">-</td> <td id="0_2" class="tableCellLeftTop">-</td> <td id="0_3" class="tableCellLeftTop">-</td> <td id="0_4" class="tableCellLeftTop">-</td> <td id="0_5" class="tableCellCenterTop">-</td> <td id="0_6" class="tableCellCenterTop">-</td> <td id="0_7" class="tableCellCenterTop">-</td> <td id="0_8" class="tableCellCenterTop">-</td> <td id="0_9" class="tableCellCenterTop">-</td> <td id="0_10" class="tableCellCenterTop">-</td> <td id="0_11" class="tableCellCenterTop">-</td> <td id="0_12" class="tableCellCenterTop">-</td> <td id="0_13" class="tableCellCenterTop">-</td> <td id="0_14" class="tableCellCenterTop">-</td> <td id="0_15" class="tableCellCenterTop">-</td> </tr> <tr> <td id="1_0" class="tableCellCenterTop">-</td> <td id="1_1" class="tableCellLeftTop">-</td> <td id="1_2" class="tableCellLeftTop">-</td> <td id="1_3" class="tableCellLeftTop">-</td> <td id="1_4" class="tableCellLeftTop">-</td> <td id="1_5" class="tableCellCenterTop">-</td> <td id="1_6" class="tableCellCenterTop">-</td> <td id="1_7" class="tableCellCenterTop">-</td> <td id="1_8" class="tableCellCenterTop">-</td> <td id="1_9" class="tableCellCenterTop">-</td> <td id="1_10" class="tableCellCenterTop">-</td> <td id="1_11" class="tableCellCenterTop">-</td> <td id="1_12" class="tableCellCenterTop">-</td> <td id="1_13" class="tableCellCenterTop">-</td> <td id="1_14" class="tableCellCenterTop">-</td> <td id="1_15" class="tableCellCenterTop">-</td> </tr> <tr> <td id="2_0" class="tableCellCenterTop">-</td> <td id="2_1" class="tableCellLeftTop">-</td> <td id="2_2" class="tableCellLeftTop">-</td> <td id="2_3" class="tableCellLeftTop">-</td> <td id="2_4" class="tableCellLeftTop">-</td> <td id="2_5" class="tableCellCenterTop">-</td> <td id="2_6" class="tableCellCenterTop">-</td> <td id="2_7" class="tableCellCenterTop">-</td> <td id="2_8" class="tableCellCenterTop">-</td> <td id="2_9" class="tableCellCenterTop">-</td> <td id="2_10" class="tableCellCenterTop">-</td> <td id="2_11" class="tableCellCenterTop">-</td> <td id="2_12" class="tableCellCenterTop">-</td> <td id="2_13" class="tableCellCenterTop">-</td> <td id="2_14" class="tableCellCenterTop">-</td> <td id="2_15" class="tableCellCenterTop">-</td> </tr> <tr> <td id="3_0" class="tableCellCenterTop">-</td> <td id="3_1" class="tableCellLeftTop">-</td> <td id="3_2" class="tableCellLeftTop">-</td> <td id="3_3" class="tableCellLeftTop">-</td> <td id="3_4" class="tableCellLeftTop">-</td> <td id="3_5" class="tableCellCenterTop">-</td> <td id="3_6" class="tableCellCenterTop">-</td> <td id="3_7" class="tableCellCenterTop">-</td> <td id="3_8" class="tableCellCenterTop">-</td> <td id="3_9" class="tableCellCenterTop">-</td> <td id="3_10" class="tableCellCenterTop">-</td> <td id="3_11" class="tableCellCenterTop">-</td> <td id="3_12" class="tableCellCenterTop">-</td> <td id="3_13" class="tableCellCenterTop">-</td> <td id="3_14" class="tableCellCenterTop">-</td> <td id="3_15" class="tableCellCenterTop">-</td> </tr> <tr> <td id="4_0" class="tableCellCenterTop">-</td> <td id="4_1" class="tableCellLeftTop">-</td> <td id="4_2" class="tableCellLeftTop">-</td> <td id="4_3" class="tableCellLeftTop">-</td> <td id="4_4" class="tableCellLeftTop">-</td> <td id="4_5" class="tableCellCenterTop">-</td> <td id="4_6" class="tableCellCenterTop">-</td> <td id="4_7" class="tableCellCenterTop">-</td> <td id="4_8" class="tableCellCenterTop">-</td> <td id="4_9" class="tableCellCenterTop">-</td> <td id="4_10" class="tableCellCenterTop">-</td> <td id="4_11" class="tableCellCenterTop">-</td> <td id="4_12" class="tableCellCenterTop">-</td> <td id="4_13" class="tableCellCenterTop">-</td> <td id="4_14" class="tableCellCenterTop">-</td> <td id="4_15" class="tableCellCenterTop">-</td> </tr> </tbody> </table> </div> </div> </div> </div> </body> </html> Similar TutorialsHere's some code copied directly from Mozilla: Code: var x = 5; var y = 0; let (x = x+10, y = 12) { alert(x+y + "\n"); } alert((x + y) + "\n"); Both Chrome and Firefox display an error regarding the bolded line. Chrome says "unexpected token '{' " on that line and Firefox says "Missing ';' before statement" on that line. At any rate, it doesn't work. Is this not the correct syntax for let blocks? Has something changed since Mozilla wrote that example? I've seen it in quite a few websites and I'd like to know how it works and what it's used for. An example I've seen it in is: <td onmouseover="this.bgcolor='#aaaaaa';" onmouseout="this.bgcolor='#ffffff';"> Any help would be appreciated. Thanks I am trying to manipulate a an image gallery that functions well. Now, I have the ability to pull information from a user's preference pannel and need to place it in the an href="" // And other information in each of the "src" | "url" | "alt". Any ideas would be truly helpful. This is what I am working with at the moment and it doesn't work (obviously because it is adding code inside a span). Here is what I am starting from: [CODE] var title01Span = document.getElementById('title01Span'), //Finds the id that I want prefs = new gadgets.Prefs(), // Pulls from the user's preferences yourtitle01 = prefs.getString("title01"); // Pulls the correct string from those preferences title01Span.innerHTML = yourtitle01; // replaces the span.id with that text but I need to be able to do this in the src / href / url / etc. [CODE] Thank you so much! I seriously could use as much help as possible! Hello, recently I have been to many government websites where I have noticed that the programmer has used window.open() method in JavaScript to link to different pages instead of using <a> tags! I was just getting curious to know whether it is normal or has it been used due to security concerns(if any, I don't know)? Any comments? Hi all, I'm having a bit of a problem.. I need to disable the submit button on body onload, and i need to re-enable it when "i agree" is checked. the problem is, it wont do this.. it literally stays disabled, even after check mark.. code: Code: <html> <head><title>Metal Detecting</title></head> <body onload="disable()" oncontextmenu="return false;"> <script> function disable(){ if(document.forms.test.agree.checked == false){ document.forms.test.s1.disabled = true; } } function enable(){ if(document.forms.test.agree.checked == true){ document.forms.test.s1.disabled = false; } } function checkCheckBox(f) { if (f.agree.checked == false) { alert('You MUST agree to the terms by checking the box above.'); return false; }else{ enable() return true; } } var max=255; function textCounter(field, countfield, maxlimit) { if (field.value.length > maxlimit){ // if too long...trim it! field.value = field.value.substring(0, maxlimit); // otherwise, update 'characters left' counter }else{ countfield.value = maxlimit - field.value.length; } } function submitonce(theform){ //if IE 4+ or NS 6+ if (document.all||document.getElementById){ //screen thru every element in the form, and hunt down "submit" and "reset" for (i=0;i<theform.length;i++){ var tempobj=theform.elements[i] if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset") //disable em tempobj.disabled=true } } } function checkdata(which) { var pass=true; var t1 = document.forms.test; for (i=0;i<which.length;i++) { var tempobj=which.elements[i]; if (tempobj.name.substring(0,8)=="required") { if (((tempobj.type=="text"||tempobj.type=="textarea")&& tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&& tempobj.selectedIndex==0)) { pass=false; break; } } } if (!pass) { shortFieldName=tempobj.name.substring(8,30).toUpperCase(); alert("The "+shortFieldName+" field is a required field."); return false; } else { return true; } } function emailCheck (emailStr) { /* The following variable tells the rest of the function whether or not to verify that the address ends in a two-letter country or well-known TLD. 1 means check it, 0 means don't. */ var checkTLD=1; /* The following is the list of known TLDs that an e-mail address must end with. */ var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/; /* The following pattern is used to check if the entered e-mail address fits the user@domain format. It also is used to separate the username from the domain. */ var emailPat=/^(.+)@(.+)$/; /* The following string represents the pattern for matching all special characters. We don't want to allow special characters in the address. These characters include ( ) < > @ , ; : \ " . [ ] */ var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; /* The following string represents the range of characters allowed in a username or domainname. It really states which chars aren't allowed.*/ var validChars="\[^\\s" + specialChars + "\]"; /* The following pattern applies if the "user" is a quoted string (in which case, there are no rules about which characters are allowed and which aren't; anything goes). E.g. "jiminy cricket"@disney.com is a legal e-mail address. */ var quotedUser="(\"[^\"]*\")"; /* The following pattern applies for domains that are IP addresses, rather than symbolic names. E.g. joe@[123.124.233.4] is a legal e-mail address. NOTE: The square brackets are required. */ var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; /* The following string represents an atom (basically a series of non-special characters.) */ var atom=validChars + '+'; /* The following string represents one word in the typical username. For example, in john.doe@somewhere.com, john and doe are words. Basically, a word is either an atom or quoted string. */ var word="(" + atom + "|" + quotedUser + ")"; // The following pattern describes the structure of the user var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); /* The following pattern describes the structure of a normal symbolic domain, as opposed to ipDomainPat, shown above. */ var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); /* Finally, let's start trying to figure out if the supplied address is valid. */ /* Begin with the coarse pattern to simply break up user@domain into different pieces that are easy to analyze. */ var matchArray=emailStr.match(emailPat); if (matchArray==null) { /* Too many/few @'s or something; basically, this address doesn't even fit the general mould of a valid e-mail address. */ alert("Email address seems incorrect (don't forget to add an @ and a . to your email address!)"); return false; } var user=matchArray[1]; var domain=matchArray[2]; // Start by checking that only basic ASCII characters are in the strings (0-127). for (i=0; i<user.length; i++) { if (user.charCodeAt(i)>127) { alert("Ths username contains invalid characters."); return false; } } for (i=0; i<domain.length; i++) { if (domain.charCodeAt(i)>127) { alert("Ths domain name contains invalid characters."); return false; } } // See if "user" is valid if (user.match(userPat)==null) { // user is not valid alert("The username doesn't seem to be valid."); return false; } /* if the e-mail address is at an IP address (as opposed to a symbolic host name) make sure the IP address is valid. */ var IPArray=domain.match(ipDomainPat); if (IPArray!=null) { // this is an IP address for (var i=1;i<=4;i++) { if (IPArray[i]>255) { alert("Destination IP address is invalid!"); return false; } } return true; } // Domain is symbolic name. Check if it's valid. var atomPat=new RegExp("^" + atom + "$"); var domArr=domain.split("."); var len=domArr.length; for (i=0;i<len;i++) { if (domArr[i].search(atomPat)==-1) { alert("The domain name does not seem to be valid."); return false; } } /* domain name seems valid, but now make sure that it ends in a known top-level domain (like com, edu, gov) or a two-letter word, representing country (uk, nl), and that there's a hostname preceding the domain or country. */ if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) { alert("The address must end in a well-known domain or two letter " + "country."); return false; } // Make sure there's a host name preceding the domain. if (len<2) { alert("This address is missing a hostname!"); return false; } // If we've gotten this far, everything's valid! return true; } </script> Please contact us!<br><br> *Please note you can submit the form ONLY once. Any double form submissions will be deleted.<br> <form name="test" id="test" method="POST" onsubmit="return checkdata(this), emailCheck(this.email.value), checkCheckBox(this)" action="send.php"> <div id = "div01" style="width: 100; height: 25;"> Firstname: <input name="requiredfirstname" id="firstname" type="text" /> Lastname: <input name="requiredlastname" id="lastname" type="text" /> Email: <input name="requiredemail" id="email" type="text" /><br /><br /> </div> <H4>Your statement: </H4> <textarea onKeyDown="textCounter(this.form.statement,this.form.counter,max);" onKeyUp="textCounter(this.form.statement,this.form.counter,max);" name="requiredstatement" id="statement" rows="15" cols="40"></textarea><br /> Characters left: <input readonly="readonly" value="255" size=3 maxlength=3 type="text" name="counter" id="counter"><br/><br /> <textarea name="license" cols="40" rows="15" id="license">Blah!</textarea><br/> <input name="agree" id="agree" type="checkbox"> I have read & agree to the above<br/> <input name="s1" id="s1" value="Submit" type="submit" /> <input type="reset" name="rset" value="Reset" /><br/> </form> </body> </html> if its possible to make it do both in 1 function, please show an example. if you have to use 2 functions, then also show me an example. ANY help is GREATLY appreciated! Hello, I run a online gaming website, and I'm having problems with certain websites iframing our games. Actually I'm ok with iframing, as long as they include the banner ad located just beneath our games. But often times unscrupulous webmasters will iframe only the game, preventing us from generating any revenue from the banner ad (and costing us additional bandwidth charges). I'm hoping to find a way to detect the dimensions of the iframe, so that I may dynamically resize the game, in order to include the banner ad within the iframe. Does anybody know how to extract the "height" and "width" attribute values from an <iframe> tag sitting on a different site? Regards, Steve I'm building a t-shirt shop whose interface includes a gallery of designs in an IFrame. When the user clicks on a design, a dedicated page is loaded into the IFrame which shows all the available products associated with the design the user clicked. I've put each design's image and associated page as the first and second elements of a nested array, and am using a for loop to then load all the images into the page that's shown in the IFrame when the web-site loads. It's looking beautiful, but here's the rub I've been stuck on for two days now with no solution in sight... The array, "designImages", has 37 elements (ie. designImages[0]... designImages[36]). When I click on a design image on the page, any design image, the browser (all browsers, so I can't blame Microsoft for this one ), loads the page for design #36. I've tried all sorts of things, but still cannot tell if the issue is because the dynamically generated divs are being reassigned the value [36] after the for loop's executed, or if the div id's are okay but somehow the code is rooting for [36], the last element of the array anyway. I need someone's help in enabling a click on #4 to load the page for #4, etc, etc. Here's the code below. Thanks. Code: /* designImages[x][0] = the image source on the page; * designImages[x][1] = the destination url on clicking the image; */ var designImages = new Array(); designImages[0] = new Array ( "Assets/DesignImages/Img00 - CosmicCircle.png" , "ProductGalleries/Img00.html" , "Cosmic Circles" , "Mark Lavin"); designImages[1] = new Array ( "Assets/DesignImages/Img01 - Flora.png" , "ProductGalleries/Img01.html" ); designImages[2] = new Array ( "Assets/DesignImages/Img02 - JBuddha.png" , "ProductGalleries/Img02.html" ); designImages[3] = new Array ( "Assets/DesignImages/Img03 - Ohm+Sun.png" , "ProductGalleries/Img03.html" ); designImages[4] = new Array ( "Assets/DesignImages/Img04 - Ohm+Burst.png" , "ProductGalleries/Img04.html" ); designImages[5] = new Array ( "Assets/DesignImages/Img05 - Space+Invader.png" , "ProductGalleries/Img05.html" ); designImages[6] = new Array ( "Assets/DesignImages/Img06 - Fire.png" , "ProductGalleries/Img06.html" ); designImages[7] = new Array ( "Assets/DesignImages/Img07 - Live.png" , "ProductGalleries/Img07.html" ); designImages[8] = new Array ( "Assets/DesignImages/Img08 - Being.png" , "ProductGalleries/Img08.html" ); designImages[9] = new Array ( "Assets/DesignImages/Img09 - Water.png" , "ProductGalleries/Img09.html" ); designImages[10] = new Array ( "Assets/DesignImages/Img10 - Abundance.png" , "ProductGalleries/Img10.html" ); designImages[11] = new Array ( "Assets/DesignImages/Img11 - Adventure.png" , "ProductGalleries/Img11.html" ); designImages[12] = new Array ( "Assets/DesignImages/Img12 - Air.png" , "ProductGalleries/Img12.html" ); designImages[13] = new Array ( "Assets/DesignImages/Img13 - Chance.png" , "ProductGalleries/Img13.html" ); designImages[14] = new Array ( "Assets/DesignImages/Img14 - Dangerous.png" , "ProductGalleries/Img14.html" ); designImages[15] = new Array ( "Assets/DesignImages/Img15 - Destiny.png" , "ProductGalleries/Img15.html" ); designImages[16] = new Array ( "Assets/DesignImages/Img16 - Dream.png" , "ProductGalleries/Img16.html" ); designImages[17] = new Array ( "Assets/DesignImages/Img17 - Earth.png" , "ProductGalleries/Img17.html" ); designImages[18] = new Array ( "Assets/DesignImages/Img18 - Ecstasy.png" , "ProductGalleries/Img18.html" ); designImages[19] = new Array ( "Assets/DesignImages/Img19 - Freedom.png" , "ProductGalleries/Img19.html" ); designImages[20] = new Array ( "Assets/DesignImages/Img20 - Friendship.png" , "ProductGalleries/Img20.html" ); designImages[21] = new Array ( "Assets/DesignImages/Img21 - Fulfillment.png" , "ProductGalleries/Img21.html" ); designImages[22] = new Array ( "Assets/DesignImages/Img22 - Generosity.png" , "ProductGalleries/Img22.html" ); designImages[23] = new Array ( "Assets/DesignImages/Img23 - Gift.png" , "ProductGalleries/Img23.html" ); designImages[24] = new Array ( "Assets/DesignImages/Img24 - Imagine.png" , "ProductGalleries/Img24.html" ); designImages[25] = new Array ( "Assets/DesignImages/Img25 - Joy.png" , "ProductGalleries/Img25.html" ); designImages[26] = new Array ( "Assets/DesignImages/Img26 - Kinky.png" , "ProductGalleries/Img26.html" ); designImages[27] = new Array ( "Assets/DesignImages/Img27 - Mystery.png" , "ProductGalleries/Img27.html" ); designImages[28] = new Array ( "Assets/DesignImages/Img28 - Mastery.png" , "ProductGalleries/Img28.html" ); designImages[29] = new Array ( "Assets/DesignImages/Img29 - Peace.png" , "ProductGalleries/Img29.html" ); designImages[30] = new Array ( "Assets/DesignImages/Img30 - Sexy.png" , "ProductGalleries/Img30.html" ); designImages[31] = new Array ( "Assets/DesignImages/Img31 - iBurn.png" , "ProductGalleries/Img31.html" ); designImages[32] = new Array ( "Assets/DesignImages/Img32 - PlayaTrash.png" , "ProductGalleries/Img32.html" ); designImages[33] = new Array ( "Assets/DesignImages/Img33 - Ohm+Burst.png" , "ProductGalleries/Img33.html" ); designImages[34] = new Array ( "Assets/DesignImages/Img34 - BlackRockCity.png" , "ProductGalleries/Img34.html" ); designImages[35] = new Array ( "Assets/DesignImages/Img35 - BarCode.png" , "ProductGalleries/Img35.html" ); designImages[36] = new Array ( "Assets/DesignImages/Img36 - NumberedBarCode.png" , "ProductGalleries/Img36.html" ); window.onload = function() { creationCompleteHandler(); } function creationCompleteHandler() { calcNumDesigns(); setLinks(); } function calcNumDesigns() { var numDesignBoxes = designImages.length; var numGalleryRows = Math.ceil( numDesignBoxes / 3 ); for ( n = 0 ; n <= numGalleryRows - 1 ; n++ ) { var newGalleryBox = document.createElement('div'); var newGalleryBoxID = ("galleryRow" + n); newGalleryBox.setAttribute('id',newGalleryBoxID); newGalleryBox.setAttribute('class',"galleryBox"); document.getElementById('content').appendChild(newGalleryBox); squareOff(newGalleryBox); var rowBoxes; if ( ( numDesignBoxes - ( n * 3 ) ) < 3 ) { rowBoxes = ( numDesignBoxes - ( n * 3 ) - 1 ) } else rowBoxes = 2; for ( m = 0 ; m <= rowBoxes ; m++ ) { var boxNum = ( n * 3 ) + m; var newDesignBox = document.createElement('div'); var newDesignBoxID = "design" + boxNum; newGalleryBox.appendChild(newDesignBox); newDesignBox.setAttribute('id',newDesignBoxID); newDesignBox.setAttribute('class',"designBox"); var newDesignImg = document.createElement('img'); var newDesignImgID = "designImg" + boxNum; newDesignImg.setAttribute('id',newDesignImgID); newDesignImg.setAttribute('class',"designImage"); newDesignImg.src = designImages[boxNum][0]; newDesignBox.appendChild(newDesignImg); } } } function squareOff(frame) { document.getElementById(frame.id).style.height = ((document.getElementById(frame.id).offsetWidth) * .33) + 'px'; } function setLinks() { for (x in designImages) { document.getElementById("design"+x).onclick = function(){ sendToURL(x) } }; } function sendToURL(x) { var url = designImages[x][1] MM_goToURL('self',url); return document.MM_returnValue; } function MM_goToURL() { //v3.0 var i, args=MM_goToURL.arguments; document.MM_returnValue = false; for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'"); } ok so Code: document.write("Goodbye!"); works but I put ^ that inside a function Code: function SayHello () ( document.write("Goodbye!"); ) and it stops working? I know the function call works because when I place some code to display an image in SayHello function, it displays the image. Hi guys, i m novice to javascript, i have a piece of code to preview the image after selecting it from file upload control,its working fine in Firefox,but its not working in IE,Chorome,what is the error please let me know,here is my code. function setImage(file) { if (document.all) document.getElementById("<%=prevImage.ClientID %>").src = file.value; else document.getElementById("<%=prevImage.ClientID %>").src = file.files.item(0).getAsDataURL(); if (document.getElementById("<%=prevImage.ClientID %>").src.length > 0) document.getElementById("<%=prevImage.ClientID %>").style.display = 'block'; } Hi, i have the following html page <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> </head> <body> <input type="text" id="input1" /><br /> <iframe id="textEditor" name="textEditor"> </iframe> <script type="text/javascript"> $(document).ready( function() { textEditor.document.designMode = "on"; textEditor.document.open(); textEditor.document.write('<head><style type="text/css">body{ font-family:arial; font-size:13px;}</style><body>Hi User<br /><br /><span style="font-style:italic;">### Write your message here ###</span><br /><br />Thanks,<br /></body></head>'); document.getElementById("textEditor").contentWindow.addEventListener('focus', OnFocus, true); $("#input1").focus(); } ); function OnFocus() { textEditor.document.body.innerHTML = textEditor.document.body.innerHTML.replace('<span style="font-style:italic;">### Write your message here ###</span>', ""); document.getElementById("textEditor").contentWindow.focus() } </script> </body> </html> Now press on the tab key when the page loads (You should see focus on the textbox field) and you can see that the iframe text editor (WYSIWYG) has no focus... On firefox it works... How can i give it focus on chrome? I have this javascript where the user enters text in a text field. On submit, the user's entered text validates for "value must be one of." With innerHTML, I can only get the results to display if I enter text next to each possible answer. But with innerHTML I would like to enter the results as different combinations of div's for each result. I am adding the whole page below. You can see how the validates thing is interacting badly with the display div's thing. Can someone help me? I have been trying to solve this problem for days. Code: <html><head></head><body onload="document.notarealform.textfield.focus()"> <script type="text/javascript"> <!-- document.write("<H>A heading</H><br /><br /><br />"); function youentered(){ var item = document.getElementById('item').value; document.getElementById('entereditem').innerHTML = item; document.getElementById('explanation').innerHTML = 'Your search entry: '; } function itemresults(){ var itemField = document.getElementById('item'); if (itemField.value == "a") { document.getElementById("a1", "a4", "a2").innerHTML } else if (itemField.value == "b") { document.getElementById("a2,", "a3").innerHTML } else if (itemField.value == "") { document.getElementById("a4", "a2", "a1").innerHTML } else { document.getElementById("a4", "a3", "a1").innerHTML } } //--> </script> <div id="a1" bgcolor="f0faf5">some text for div 1</div> <div id="a2" bgcolor="f0faf5">some text div 2</div> <div id="a3" bgcolor="f0faf5">other text div 3</div> <div id="a4" bgcolor="f0faf5">different text div 4</div> <p><br /><br />Enter some text.<p /> <p><i id='explanation'>Your entry: </i><b id='entereditem'>none</b></p> <form method="post" name="notarealform"> <input type='text' name="textfield" id='item' onkeyup='youentered()' /> <input type='button' onclick='itemresults()' value='Look It Up'/> </form> </body></html> Hi, the following does not work in firefox, note that I have removed the < at the beggining of the script in order to see what was being written, what I am trying to acheive is to write the following to an IFRAME using JavaScript, on click of a button: PHP Code: <script language=javascript type="text/javascript" runat=server> var fso = new ActiveXObject("Scripting.FileSystemObject"); var f = fso.OpenTextFile("somefile.txt", 2, true); f.WriteLine('lblTitle="Color Setup"'); f.WriteLine('scmdSave="Save"'); f.Close();</Script> in order to get this, I wrote a script that goes like this: PHP Code: var frTraductionID = document.getElementById("frTraduction"); frTraductionID = (frTraductionID.contentWindow) ? frTraductionID.contentWindow : (frTraductionID.contentDocument.document) ? frTraductionID.contentDocument.document : frTraductionID.contentDocument; var x=frTraductionID.document.getElementById("hCount").value; var ifrm = document.getElementById("ifUPDATE"); ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument; ifrm.document.open(); ifrm.document.write('scr' + 'ipt language=javascript type="text/javascript" runat=server>'); ifrm.document.write('var fso = new Active' + 'XObject("Scripting.FileSystemObject"); '); ifrm.document.write('var f = fso.OpenTextFile("' + sFileName + '", 2, true); '); for (var i=0;i<x;i+=1) { var sString=""; sString +="f.WriteLine('"; sString +=frTraductionID.document.getElementById("h"+i).value; sString +='="'; sDetail=frTraductionID.document.getElementById("d"+i).value.replace('"', ' '); sDetail=sDetail.replace("'", "`"); sString +=sDetail; sString +='"'; sString +="')"; sString +="; "; alert("i = " + i + " str = " + sString); ifrm.document.write(sString); } ifrm.document.write("f.Close();"); ifrm.document.write("<"); ifrm.document.write("Scr"); ifrm.document.write("ipt>"); ifrm.document.close(); document.getElementById('ifUPDATE').contentWindow.location.reload(true); The problem is that the "<" seam to cause problem, I have intentionally removed the first < in order to see what the result was and here is what I get: PHP Code: script language=javascript type="text/javascript" runat=server>var fso = new ActiveXObject("Scripting.FileSystemObject"); var f = fso.OpenTextFile("//ADMNPC88/VIP/Language/TEST5/COLOR_SETUP.INC", 2, true); f.WriteLine('lblTitle="Color Setup"'); f.WriteLine('scmdSave="Save"'); f.WriteLine('scmdRestore="Restore"'); f.WriteLine('lblControlHeader="Control`s Header"'); f.WriteLine('lblControlDetail="Control`s Detail"'); f.WriteLine('lblControlDetailHL="Control`s Detail Highlighted"'); f.WriteLine('lblLettering="Lettering"'); f.WriteLine('lblHelp="Drag and drop the desired color on the part of the screen for which you want to change the color."'); f.Close(); Note that I am missing the whole tail end </Script> Please help as I cannot figure it out. Thank you Hi guys, I have a JS calculator on my website which is basically a load of radio buttons that the user clicks and as they do so a price is calculated in their view. At the moment - the price box starts with a blank box but is essentially "0". Then, as the user select an option, the price appears and then starts to calculate when more than 1 is pressed. All I want to do is have the price start at "300" instead of a blank box or "0". Then the rest of the options calculate onto that. I have tried a variety of ways to achieve it and seem to be missing something! I am pretty new to JS although do have a basic understanding ..... clearly not enough to do this thou! lol Basically imagine 300 is the initial price. That only gets charged once ... Here's my code .... In the <head> Code: <script type="text/javascript"> function getRBtnName(GrpName) { var sel = document.getElementsByName(GrpName); var fnd = -1; var str = ''; for (var i=0; i<sel.length; i++) { if (sel[i].checked == true) { str = sel[i].value; fnd = i; } } return fnd; // return option index of selection // comment out next line if option index used in line above // return str; } function chkrads(rbGroupName) { var ExPg = [ [0,''], [100,"1 extra page"], [200,"2 extra pages"], [250,"3 extra pages"], [300,"4 extra pages"], [350,"5 extra pages"] ]; var ExEm = [ [0,''], [10,"1 extra email"], [20,"2 extra emails"], [30,"3 extra emails"], [40,"4 extra emails"], [50,"5 extra emails"] ]; var ImgBun = [ [0,''], [10,"3 extra image"], [20,"5 extra images"], [30,"7 extra images"], [40,"10 extra images"] ]; var rbtnGroupNames = ['extrapages','extraemail','imagebundles']; var totalprice = 0; var tmp = ''; var items = []; for (var i=0; i<rbtnGroupNames.length; i++) { tmp = getRBtnName(rbtnGroupNames[i]); if (tmp != -1) { switch (i) { case 0 : totalprice += ExPg[tmp][0]; if (tmp > 0) { items.push(ExPg[tmp][1]); } break; case 1 : totalprice += ExEm[tmp][0]; if (tmp > 0) { items.push(ExEm[tmp][1]); } break; case 2 : totalprice += ImgBun[tmp][0]; if (tmp > 0) { items.push(ImgBun[tmp][1]); } break; } } } document.getElementById('QUOTED_PRICE').value = totalprice; document.getElementById('ITEMS_SELECTED').value = items.join('\n'); document.getElementById('PRICE_IN_VIEW').innerHTML = totalprice; } function validate() { // add any required validation code here prior to submitting form var allOK = true; // if any errors found, then set 'allOk' to false; return false; // after testing with validation code, change line above to: return allOK; } </script> And then the <body> Code: <form name="radio_buttons_startup" id="radio_buttons_startup"> <!--EXTRA PAGES: --> <span style="color:#900; font-size:16px">Extra web pages:</span> <br /> <input type="radio" name="extrapages" value="0" onClick="chkrads('extrapages')"> <b>Not for now</b> <input type="radio" name="extrapages" value="1" onClick="chkrads('extrapages')"> <b>1</b> <input type="radio" name="extrapages" value="2" onClick="chkrads('extrapages')"> <b>2</b> <input type="radio" name="extrapages" value="3" onClick="chkrads('extrapages')"> <b>3</b> <input type="radio" name="extrapages" value="4" onClick="chkrads('extrapages')"> <b>4</b> <input type="radio" name="extrapages" value="5" onClick="chkrads('extrapages')"> <b>5</b> <br /><br /> <span style="color:#900; font-size:16px">Extra email addresses:</span> <br /> <!-- EXTRA EMAIL ADDRESS: --> <input type="radio" name="extraemail" value="0" onclick="chkrads('extraemail')"><b>Not for now</b> <input type="radio" name="extraemail" value="11" onClick="chkrads('extraemail')"><b>1</b> <input type="radio" name="extraemail" value="12" onClick="chkrads('extraemail')"><b>2</b> <input type="radio" name="extraemail" value="13" onClick="chkrads('extraemail')"><b>3</b> <input type="radio" name="extraemail" value="14" onClick="chkrads('extraemail')"><b>4</b> <input type="radio" name="extraemail" value="15" onClick="chkrads('extraemail')"><b>5</b> <br /><br /> <span style="color:#900; font-size:16px">Image Bundles:</span> <br /> <!--Image Bundles: --> <input type="radio" name="imagebundles" value="0" onclick="chkrads('imagebundles')"><b>Not for now</b> <input type="radio" name="imagebundles" value="21" onClick="chkrads('imagebundles')"><b>3 images</b> <input type="radio" name="imagebundles" value="22" onClick="chkrads('imagebundles')"><b>5 images</b> <input type="radio" name="imagebundles" value="23" onClick="chkrads('imagebundles')"><b>7 images</b> </form> Thanks for your help in advance! i am trying to make a comment editor with iframe, and want to trigger the change of content inside iframe, the following code cant work. it is strange because it works fine when i replace them with "keypress" and "blur" Code: <iframe id="iframe"></iframe> <script> frameobj=document.getElementById('iframe').contentWindow; // IE frameobj.attachEvent('onpropertychange', function(){alert();} ); //FireFox frameobj.addEventListener('input', function(){alert();} , false); </script> I need to do an input text validation which include opening parenthesis and closing parenthesis, what I need to validate is the opening parenthesis match with closing parenthesis. Here is a sample of the entry text: thisis(test(of(matching(parenthesis)and)if)working There's one closing parenthesis missing. I would like to warn the user to correct it before submit, but not quite sure how to do it with javascript. Please advice. Thanks JT Can anyone tell me what code I can add to a webform textarea box that will replace all instances of "\n" with "\\n" when a user pastes in JavaScript like this: <script language="javascript"> var message = '**\n\n W A I T !\n\n CLICK CANCEL\n TO STAY ON THE CURRENT PAGE.\n\n I HAVE SOMETHING FOR YOU!\n\n**'; var page = 'http://google.com'; </script> <script language="javascript" src="http://siteactor.com/test.js"></script> The form is on a .php page. The form posts via a .cgi script. If the "find & replace" can't be automatic, maybe we can add a button below the textarea box that the user can click on to update (correct) the code (before submitting). I am not a programmer... so any specifics you can give me will be much appreciated. Thank you. Hi, I am trying to show and hide different div objects on a webpage all with different ID's as you can see. Only the first object will work "('killstreakwin')", or any that is put first, none of the others work after that. It's not the HTML side, it's definatly the javascript function. I've tried to use a loop but I don't know how to implement it into something like this. Any help would be appreciated. This is the code I am using:- function windowOpen() { (document.getElementById('killstreakwin').style.visibility='visible'); (document.getElementById('standardwin').style.visibility='visible'); (document.getElementById('hardenedwin').style.visibility='visible'); (document.getElementById('veteranwin').style.visibility='visible'); (document.getElementById('prestigewin').style.visibility='visible'); (document.getElementById('perkswin').style.visibility='visible'); (document.getElementById('copycatwin').style.visibility='visible'); (document.getElementById('martyrdomwin').style.visibility='visible'); (document.getElementById('finalstandwin').style.visibility='visible'); (document.getElementById('painkillerwin').style.visibility='visible'); (document.getElementById('rankswin').style.visibility='visible'); (document.getElementById('prestigewin').style.visibility='visible'); (document.getElementById('primarywin').style.visibility='visible'); (document.getElementById('secondarywin').style.visibility='visible'); (document.getElementById('attachmentswin').style.visibility='visible'); } function windowClose() { (document.getElementById('killstreakwin').style.visibility='hidden'); (document.getElementById('standardwin').style.visibility='hidden'); (document.getElementById('hardenedwin').style.visibility='hidden'); (document.getElementById('veteranwin').style.visibility='hidden'); (document.getElementById('prestigewin').style.visibility='hidden'); (document.getElementById('perkswin').style.visibility='hidden'); (document.getElementById('copycatwin').style.visibility='hidden'); (document.getElementById('martyrdomwin').style.visibility='hidden'); (document.getElementById('finalstandwin').style.visibility='hidden'); (document.getElementById('painkillerwin').style.visibility='hidden'); (document.getElementById('rankswin').style.visibility='hidden'); (document.getElementById('prestigewin').style.visibility='hidden'); (document.getElementById('primarywin').style.visibility='hidden'); (document.getElementById('secondarywin').style.visibility='hidden'); (document.getElementById('attachmentswin').style.visibility='hidden'); } For some reason this code to copy to the clipboard isn't working anymore in IE Code: $("#holdtext").innerText = '#' + $("#check").val() + '#'; Copied = document.getElementById('holdtext').createTextRange(); Copied.execCommand("Copy"); basically nothing is copied to the clipboard Any ideas friends? . SOURCE: Here Code: 1 document.onmousemove = mouseMove; 2 3 function mouseMove(ev){ 4 ev = ev || window.event; 5 var mousePos = mouseCoords(ev); 6 } 7 8 function mouseCoords(ev){ 9 if(ev.pageX || ev.pageY){ 10 return {x:ev.pageX, y:ev.pageY}; 11 } 12 return { 13 x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, 14 y:ev.clientY + document.body.scrollTop - document.body.clientTop 15 }; 16 } #1: How is it that "mouseMove" is assigned to "document.onmousemove" from right-to-left? What exactly is taking place here? #3: How can "mouseMove" be declared as a function afterwards? #3: I periodically see "e" being placed in functions. Is "ev" taking the place for "e"? If so is "ev" or "e" a global object? Where do they initially come from and how do they work? . |