JavaScript - Tricky (for Me) Javascript App
Hello,
I'm pretty new to javascript (I work mostly in AS3 but am starting to learn my way around js) and I'm hitting a wall on an app build I'm working on. The build is pretty simple, I need to combine the values of a selected radio button and a selected drop down field and output a their combined value plus an additional value. This is for a pick your package vacation. A user can combine golfing with a specific location and below I'd like to populate the cost of such a trip. While I am outputting the values of the radiobuttons and input fields, I cannot get these values to combine or select a third value (the cost of the combined values). It's a bit messy but this is what I have so far. Code: <body> <div id="greenBox"> <div id="whiteBox"> <img src="assets/pickAndSaveGetawayPackages.jpg" id="pickAndSave"/> <img src="assets/happyFam.jpg" id="happyFamily"/> <!--Parks Dropdown Menu--> <div id="pickAPark"> <FORM name="dropDown" method="post" action="dummy" id="dropDown"><left> <select name="myList" size="1" onChange="disp_text()"> <option value="default" name="default" id="default" onclick="alertPark();" selected >Pick a Park</option> <option value="p1" name="p1" id="p1">George T. Bagby</option> <option value="p2" name="p2" id="p2">Park #2</option> <option value="p3" name="p3" id="p3">Park #3</option> <option value="p4" name="p4" id="p4">Park #4</option></select></left></FORM></div><!--pick a park--> <!--Packages Dropdown Menu--> <div id="pickAPackage"> <FORM name="radioForm" id="radioExampleForm" method="get" action="" onsubmit="return false;"> <div align="left"><!--Align Left--><br> <label for="number1"><input type="radio" value="Cottage & Canoe" name="number" id="number1"></label> <br> <label for="number2"><input type="radio" value="Lodge & Learn" name="number" id="number2"></label> <br> <label for="number3"><input type="radio" value="Golf Play & Stay" name="number" id="number3"></label> <br> <label for="number4"><input type="radio" value="RV Explorer" name="number" id="number4"></label> <p><input type="button" onclick="alert('Checked value is: '+getCheckedValue(document.forms['radioExampleForm + dropDown'].elements['number']));" value="Submit"> </FORM></div><!--Align Left--></div><!--pick a package--> <div id="infoBox"> </div><!--infoBox--> <img src="assets/toBook.jpg" id="toBook"/> </div><!--whiteBox--> </div><!--greenBox--> </body> and on the js side //Drop Down Menu function disp_text() { var w = document.dropDown.myList.selectedIndex; var selected_text = document.dropDown.myList.options[w].text; alert(selected_text); } //Return value of checked radio button //Return empty string if none are checked, function getCheckedValue(radioObj) { if(!radioObj) return ""; var radioLength = radioObj.length; if(radioLength == undefined) if(radioObj.checked) return radioObj.value; else return ""; for(var i = 0; i < radioLength; i++) { if(radioObj[i].checked) { return radioObj[i].value; } } return ""; } //Do nothing if none of radio buttons are checked //If the given value does not exist, all the radio buttons //Reset to unchecked function setCheckedValue(radioObj, newValue) { if(!radioObj) return; var radioLength = radioObj.length; if(radioLength == undefined) { radioObj.checked = (radioObj.value == newValue.toString()); return; } for(var i = 0; i < radioLength; i++) { radioObj[i].checked = false; if(radioObj[i].value == newValue.toString()) { radioObj[i].checked = true; } } } Anything would be tremendously helpful! Thanks! Similar Tutorialsi have an admin page with lots of dropdown menus. on one of them it has lots of colors in but the owner would like to be able to put special car colors in if he needs to. so i said i could have the dropdown menu replace itself with a text input if the value was "other". first of is this even possible and secondly how would i go about this. because of the perl script i have set up i would like the id and name to be the same on the dropdown menu and the text input I am trying to run a simple replace over some css a user puts into a textarea. I would like to remove all the comments Code: /* MY COMMENT */ I have tried the following to try and escape the forward slashes but to no avail. Code: replace(/\/*(.*)\//g,""); any ideas? Thanks! Hi there, I'm looking to create something similar to the online javascript terminal located at http://cb.vu I don't know very much about it so I guessed I'd post here and hope to find someone who knows what they're doing. Thanks! Hi all, I have a fairly tricky problem: I have an XML feed of some products from a shop. Is there any way to make it so that if a link to 'product 1' (product_id=0001) is clicked then a div appears elsewhere on the page displaying information about that product from the XML feed (eg stock level and price of 0001). However, when a link to 'product 2' (product_id=0002) is clicked then then the information in this div is that of product 2. (ie: not opening a new div elsewhere or over the top of the old one for product 2's info, but actually in the original div, replacing product 1's info. I guess AJAX is the best method?) The reason i want to do this is so that the info displayed in the div is dependant on the link that has been clicked. I will have links to up to a few hundred products on the page so this method would keep the script size and loading times down - not to mention the enormous amount of work of manually coding a new div for each product. In fact... to make it even more complicated i actually want the links themselves (‘product 1’, ‘product 2’ etc…) to be in an iframe but the div to be opened on the parent page... perhaps its best to tackle on thing at a time though! The XML looks like: Code: <Products> <Product> <Main_Category_id>1</Main_Category_id> <Main_Category_Name>products</Main_Category_Name> <Sub_Category_1_id>12</Sub_Category_1_id> <Sub_Category_1_Name>products sub-category</Sub_Category_1_Name> <Product_id>0001</Product_id> <Product_Model>abcde</Product_Model> <Product_Name>Product 0001</Product_Name> <Product_Description> Description of product 1 </Product_Description> <Product_Price>5.00</Product_Price> <Product_Price_Special>0.00</Product_Price_Special> <Product_Currency>GBP</Product_Currency> <Product_Vat_Rate>0</Product_Vat_Rate> <Product_Image>0001</Product_Image> <Product_Image_Url> http://www.mysite.com/0001.jpg </Product_Image_Url> <Product_Quantity>9999</Product_Quantity> <Product_Weight>0.12</Product_Weight> <Product_Status>1</Product_Status> <Product_Manufacturer/> <Product_Last_Modified/> <Product_Date_Available/> </Product> <Product> <Main_Category_id>1</Main_Category_id> <Main_Category_Name>products</Main_Category_Name> <Sub_Category_1_id>12</Sub_Category_1_id> <Sub_Category_1_Name>products sub-category</Sub_Category_1_Name> <Product_id>0002</Product_id> <Product_Model>abcde</Product_Model> <Product_Name>Product 0002</Product_Name> <Product_Description> Description of product 2 </Product_Description> <Product_Price>10.00</Product_Price> <Product_Price_Special>0.00</Product_Price_Special> <Product_Currency>GBP</Product_Currency> <Product_Vat_Rate>0</Product_Vat_Rate> <Product_Image>0002</Product_Image> <Product_Image_Url> http://www.mysite.com/0002.jpg </Product_Image_Url> <Product_Quantity>9999</Product_Quantity> <Product_Weight>0.12</Product_Weight> <Product_Status>1</Product_Status> <Product_Manufacturer/> <Product_Last_Modified/> <Product_Date_Available/> </Product> Etc… </Products> Thanks for any help at all Matt Hello guys, first post so please be kind. Usually try and find a solution and edit to work for me but this time its a little trickier. I need a script and code that can toggle the contents of a div, while changing the button appearance but !! the button images and content have to be external to the script as the content and images will be loaded from a database on the page and duplicated on page. ideally what I want is say 3 buttons. button1img button2img button3img <// that when clicked control <div id=target><this target content is now set to 2></div> //and toggle between options and only one at a time can be on. The button images and content have to be set outside of the initial script (reason it will be duplicated several times on page) and it cant be a call page 'content1.html' either as it needs to be created from a db on the page. if anyone can help it will be MUCH appreciated, as im pulling my hair out trying to combine 3 different scripts that are all written differently. i understand this is a little trickier, im hoping it is possible. cheers steve I want to have another go at Javascript. I have several books on the subject but I find that my eyesight is a major problem. Therefore I want to try an on-line solution, preferably free. I have Googled, but there are so many that I am almost dizzy with the choices. Perhaps someone could recommend one. Not too fussy visually. My knowledge is VERY basic. Frank Hello! I am trying to find a script that allows you to open multiple browser tabs and then close each of those tabs, either one by one or all at once. Does anyone know how to do this please? Thanks so much for your help. Does anyone know how to make URL links that use Javascript still work when users have Javascript disabled on their browser? The only reason I'm using JS on a URL is because my link opens a PDF file, and I'm forcing it not to cache so users have the latest version. I tried the <script><noscript> tags, but I'm not sure if I'm using it correctly, as my URL completely disappears. Below is my HTML/Javascript code: <p class="download"> <script type="text/javascript">document.write("<span style=\"text-decoration: underline;\"><a href=\"javascript:void(0);\" onclick=\"window.open( 'http://www.webchild.com.au/mediakit/Direct_Media_Kit_Web.pdf?nocache='+ Math.floor( Math.random()*11 ) );\" >The Child Magazines Media Kit</a></span> (PDF 1 MB) ");</script> <noscript><span style="text-decoration: underline;"><a href="http://www.webchild.com.au/mediakit/Direct_Media_Kit_Web.pdf" >The Child Magazines Media Kit</a></span> (PDF 1 MB)</noscript> </p> Thanks for any help, Michael Hi, I have the following code snippet: test.html ====== <script language="javascript" type="text/javascript"> var testVariable = "test"; </script> <script language="javascript" type="text/javascript" src="test.js"> </script> test.js ===== var testVariable = window.top.testVariable; In firefox, I'm able to access testvariable defined within test.html in test.js. But in chrome, test.js couldnot get the window.top.testVariable field defined in test.html. Can any one please let me know how i can make it work in chrome?. Am i missing something here?. Hi Guys, I am new at JavaScript and start to do some tutorials.What I am trying to do here is prompting user to input a name and if the name was valid the page(document) will display with all objects like the button.But if user enter a wrong name then the button will be disabled! I create the following code but it did not work <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>New Web Project</title> <script language="JavaScript" type=""> function changeColor(){ document.bgColor = "Gray"; } </script> </head> <body> <script language="JavaScript" type="text/javascript"> var person = ""; person = prompt('What is Your Name:'); if (person == "Foo") { document.write("<h1 />Welcome " + person); document.bgColor = "Yellow"; } else { document.write("<h1 />Access Denied!!!!"); document.bgColor = "Red"; document.getElementById("gree").disabled = true; } </script> <div> <p/><input id="gree" type="button" value="Gray " onClick="changeColor();"> </div> </body> </html> as you can see I used the: document.getElementById("gree").disabled = true; but it did not work , could you please give an idea how I can solve this problem? Thanks Hi Guys I am trying to modify the functionality of my page. I want to be able to activate this piece of code using another javascript function. This is the code I want to activate: Code: <script type="text/javascript"><!-- $('#button-cart').bind('click', function() { $.ajax({ url: 'index.php?route=checkout/cart/update', type: 'post', data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea, .date_data input[type=\'text\']'), dataType: 'json', success: function(json) { $('.success, .warning, .attention, information, .error').remove(); if (json['error']) { if (json['error']['warning']) { $('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>'); $('.warning').fadeIn('slow'); } for (i in json['error']) { $('#option-' + i).after('<span class="error">' + json['error'][i] + '</span>'); } } if (json['success']) { $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>'); $('.success').fadeIn('slow'); $('#cart_total').html(json['total']); $('html, body').animate({ scrollTop: 0 }, 'slow'); } } }); }); //--></script> And this is how I want the format of the function to be: function testsession() { if there is a session called 'hiredate' { activate the script above } else { var el = document.getElementById("product_data"); } } I just dont know how to write this in javascript Could you help me if possible please I want to insert this js snippet Code: function addText(smiley) { document.getElementById('message').value += " " + smiley + " "; document.getElementById('message').focus(); return false; } to a loaded iframe with name&id chtifrm. I can access it & change embed something in its html via using something like: Code: $(parent.chtifrm.document.body).append('<div id=\"smly\" style=\"cursor:pointer;float:left;top:200px;display:none;position:absolute;\"><\/div>'); .... Code: parent.chtifrm.document.getElementById('chatbox_option_disco').style.display == 'none' but how do I insert js in the head of loaded iframe? Hey, I've got to make the values of some textboxes change the co-ordinates of my sprite on a canvas and havent a clue on how to do it, Here is my form with the two textboxes and submit button: <form> x: <input type="text" name="x" /><br /> y: <input type="text" name:"y" /><br /> <input type="submit" value="Submit"/><br /> </form> And i need it so that they change the values of these: //this shows where my sprite will start on the canvas var block_x; var block_y; searched the internet for hours and cant really find anything i understand or works. any help is much appreciated I got an index.php Code: <html> <form action="bacakomik.php" method='post'> <select name="kodekomik"> <option value='../komik1/|23'>Judul Komik1</option> <option value="../komik2/|20">Judul Komik2</option> <option value="../komik3/|10">Juduk Komik3</option> <option value="../komik4/|20">Judul Komik4</option> </select> <input type="submit" /> </form> <?php echo ('<select>'); echo ('<option value= "'.$i.'">'.'Page '.$i.'</option>'); echo ('</select>'); ?> </html> As you can see, each of the option brings specific value "../komik1/|23" komik1 is a directory | is a delimiter 23 is the pages in one chapter and can be considered also as how many images are there on a specific directory This is my bacakomik.php Code: <?php $dirkomik = $_POST['kodekomik']; $exploded = explode("|", $dirkomik); echo ($exploded[0]); //picture directory echo ("<br>"); echo ($exploded[1]); //total page in the comic $pagecount = (int)$exploded[1]; //Take last posted value, process it right away echo ('<FORM name="guideform"> '); echo ('<select name="guidelinks">'); $i=1; do { echo ('<option value= "'.$i.'">'.'Page '.$i.'</option>'); $i= $i+1; }while($i <= $pagecount); //Printing option and select echo ("</select>"); ?> <input type="button" name="go" value="Go!" onClick="document.getElementById('im').src=document.guideform.guidelinks.options[document.guideform.guidelinks.selectedIndex].value+'.png';"> </FORM> <img src="img0.jpg" id="im"> With the current code on bacakomik.php, I only can change the img src of id "im" in the same directory only. What I want is that the Javascript could "add" the "$exploded[0]" variable so that the picture can be loaded from different directory. Anyone can do this? I believe that the fix should be somewhere on input tag inside OnClick, or do you know where? Anyway, I found this on the net http://p2p.wrox.com/php-faqs/11606-q...avascript.html Please help me to those who can... All -- I have a JavaScript config file called gameSetting.js which contains a bunch of variables which configures a particular game. I also have a shared JavaScript library which uses the variables in gameSetting.js, which I include like so: <script type="text/javascript" src="gameSetting.js" ></script> <script type="text/javascript" src="gameLibrary.js" ></script> In gameSetting.js I have: $(document).ready(function() { // call some functions / classes in gameLibrary.js } in Firefox, Safari, and Chrome, this works fine. However, in IE, when it's parsing gameSetting.js, it complains that the functions that live in gameLibrary.js aren't defined. When it gets to parsing gameLibrary.js, the variables in gameSetting.js are reported as not being defined. I've tried dynamically bootstrapping the gameLibrary file using this function in document.ready for dynamic load... $.getScript("gameLibrary.js"); However, the same problem still happens in IE, where when it parses the files individually it's not taking into context the file/variables that came before, so it's not an out of load order problem. My options a 1) collapsing all the functions in gameLibrary.js and variables in gameSetting.js into one file. However, this is not practical because this is dealing with literally hundreds of games, and having a gameLibrary.js in ONE location for ONE update is what makes most logical sense. 2) figure out a way to get this to work where variables in file1 are accessible to file2 in IE (as it seems they are in other browsers). jQuery seems to be able to have multiple plugins that all refer to the based jQuery-1.3.2.js, so I know there is a way to get this to work. Help appreciated. Nero I wrote a log function that took note of various function calls. Thinking that functions are first class objects, and objects have properties, I made the name of each logged function a property of that function, e.g., brightenInnerPara.name = "brightenInnerPara"; Every browser I tried (Firefox, MSIE, Opera, Chrome, Safari) accepted the assignment, no problem. In Firefox and MSIE, the result was what I wanted: brightenInnerPara.name == "brightenInnerPara" But in the others, the result was: brightenInnerPara.name == null Question 1. Which Javascript is correct here? I favor Firefox and MSIE, not merely because they were willing to give me what I wanted, but also because it makes no sense to accept an assignment statement without throwing an error and then give it a null semantics, like Chrome, Opera, and Safari did. I found a workaround, using assignments like this: brightenInnerPara.prototype.name = "brightenInnerPara"; To my surprise, that worked in every browser. But I don't know why. It seems that such assignments are enough to cause each function to have its own distinct prototype. Question 2. Just how inefficient is my workaround, and why does it work? Hi everyone, can someone please help me to find a way to rewrite this code function evenorodd( form ) { var x = new Number( form.x.value ); if( isNaN( x ) || x == "" ) { alert( "Not a number or no value entered" ); return ""; } for( var i = 0; i <= x; i++ ) ( i % 2 == 0 ) ? alert( i + " is even!" ) : alert( i + " is odd!" ); } the purpose is to show an alert box with a odd or even number Thanks in advanced! I have a calendar I made myself. What I want is to open it in a new window, each date is a link, when clicked the date is returned to the original page. How do I do that?? I have searched and found that it is not to hard to make the calendar to open in a new window, but how do I fix the date to be passed along on click?? Yeah, forgot to say I want the date to be returned to a form field... Hi everyone, I'm trying to generate a drop down list using a supplied xml form with an xsl sheet. Because xsl lacks commands that will allow me to create repetitive <options> I am using javascript. A sample xml file that I am using is bellow. Code: <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="a.xsl"?> <Country> <California> <County>San Diego</County> <County>Los Angeles</County> </California> <Nevada> <County>Reno</County> <County>Las Vegas</County> </Nevada> </Country> The xsl file that I'm using is show below. Code: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head></head> <body> <p>This loaded</p> <form> <select name='County'> <script type="text/javascript"> var i=3; while(i > 0){ document.write("<option><xsl:value-of select='Country/California/County["); document.write(2); document.write("]'/></option>"); i--; } </script> </select> </form> <script type="text/javascript"> var i=3; while(i > 0){ document.write("<xsl:value-of select='Country/California/County["); document.write(2); document.write("]' /> <br />"); document.write("<xsl:value-of select='Country/California/County[2]'/><br />"); i--; } </script> </body> </html> </xsl:template> </xsl:stylesheet> Bassically all I'm trying to do is list the counties under california. My problem is that the javascript will print the list no problem but it only prints san diego even if my xpath index = i, is set to two. When i is set to 2, it should display Los Angeles. The following sample code will display Los Angeles Code: document.write("<xsl:value-of select='Country/California/County[2]' />"); However when the document has i = 1, it should display san diego. Unfortunately when i use i = 2 and write ... Code: document.write("<xsl:value-of select='Country/California/County[" document.write(i); document.write("]' />"); or even Code: document.write("<xsl:value-of select='Country/California/County["); document.write(2); document.write("]' />"); The xsl sheet will display san diego instead. If the code is used without brackets you can see that everything is being displayed correctly ... for instance writting Code: document.write("xsl:value-of select='Country/California/County[" document.write(i); document.write("]' /"); will result in .. Code: xsl:value-of select='Country/California/County[2]' / Why is xsl doing this to me? |