JavaScript - Help With Substring
Hey Guys,
I'm looking to code something in JavaScript within an HTML page that asks the user for two numbers. When the user clicks Calculate, it subtracts them and multiplies the difference by 3. I've gotten that to work fine. However, I run into some trouble because the user may enter 31000, and the formula only works with 31. (So, if the user enters 31000 and 11000, the code has to cut those into 31 and 11 to plug into the formula.) I know I'll have to use (var).substring(0,1) but when I do this the program doesn't seem to run. I'm not sure if this isn't working because it's within <HEAD></HEAD> or because it's looking for letters and I only have integers in the variable. Here's the code I'm currently working with: Any help on how to extract the first two digits from "currentaltitude" and "finalaltitude" is appreciated. Thanks in advance!! Code: <html> <head> <script> function temp(form) { var currentaltitude = parseFloat(form.curAltitude.value, 10); var finalaltitude = parseFloat(form.finAltitude.value, 10); var calcaltitude = parseFloat(form.calcAltitude.value, 10); calcaltitude = (currentaltitude - finalaltitude) * 3; form.calcAltitude.value = calcaltitude; } </script> </head> <body> <FORM> Enter current altitude: <INPUT TYPE=text NAME="curAltitude" VALUE="" MAXLENGTH="15" SIZE=10> <p> Enter final altitude: <INPUT NAME="finAltitude" VALUE="0" MAXLENGTH="15" SIZE=10> <p> Click this button to calculate descent altitude: <INPUT NAME="calc" VALUE="Calculate" TYPE=BUTTON onClick=temp(this.form)> <p> Begin descending <INPUT NAME="calcAltitude" READONLY SIZE=4> DME from the assigned crossing point. </FORM> </body> </html> Similar TutorialsBasically what i'm trying to do is check the data in an XML node to see if it contains a certain string. I'm completely new to javascript and this seems like something very simple, but I must not be using the correct terms in my searches. For instance here i'm checking to see if the data in an xml node is equal to "URGENT" if so I'm changing the css. Code: function DoesXmlContainThisNode(strXML, strNodeXPath) { var bolFlag // Load XML var myDOMDocument = new ActiveXObject('Msxml2.DOMDocument.3.0'); myDOMDocument.async = false; myDOMDocument.loadXML(strXML); // Parse out value var myNode = myDOMDocument.selectSingleNode(strNodeXPath) if( myNode != null ) { bolFlag = true } else { bolFlag = false } myNode = null return bolFlag } , Code: if (DoesXmlContainThisNode(strResponseXml, "Response/XMLResponse/*/Message/Body[text()='URGENT']")) { strStyle = 'URGENT' } And maybe I cant do it the same way, but how would I search for any instance of the word "urgent". Say my message has 4 sentences and if the wor durgent appears anywhere in it.. I want to change the stylesheet. I would think there is some way to use a wildcard or "contains" string... I see the text() class being used everywhere but only on exact matches. Any help is greatly appreciated. Thanks Hi all, I am having trouble trying to use the substring String method. Relevant HTML code: Code: <img id="news" src="../images/news.png" alt="News" onmouseover="change(this)" /> Relevant JavaScript code: Code: function change(element) { if (element.id == "news" && element.src.substring(3,5) != "../") { element.src = "../images/news.png"; alert(element.src.substring(3,5)); } else if (element.id == "news" && element.src.substring(3,5) == "../") { element.src = "../../images/news.png"; } } I want the image to change to another image, but depending on the src in the HTML code. This is so that it can locate the correct folder. However, it doesn't work, and I tried using an alert statement to show me what JavaScript is reading and it seems to read "e:", which is not correct. It never reads "../", which it should. I don't know how to solve this problem, any ideas? Hi Guys, I am trying to make a function that will search for a substring, in this case 'http://instagr.am/p/' within a text area on a form then add '[embed]' & '[/embed]' tags around it. I have currently got the following code Code: <script type='text/javascript'> function show_alert() { str = formname.elements['inputid'].value; arr = (str.split(' ') + '<br />'); jQuery.each(arr, function() { if (arr.indexOf('http://instagr.am/p/') >= 0) { alert('An http://instagr.am/p/ link has been found'); //alert('It is entry ... in the array'); //Then edit the entry } } ); } </script> This breaks the contents of the textarea (at spaces) into an array then finds any entries with 'http://instagr.am/p/' in them (could be 'http://instagr.am/p/29fdghHdv'). Once it has found any and all of these entries it will add an '[embed]' code to the beginning and an '[/embed]' tag at the end. Example... http://instagr.am/p/vuHdeyfa2 is converted to: '[embed]http://instagr.am/p/vuHdeyfa2[/embed]' Then the changes must reflected in the textarea input "formname.elements["textareaid"].value = editedstring; Thankyou for your assistance. EDIT: Great...I already screwed up the title. It's not a div class but anchor class :s I've got previous programming experience but unfortunately none with javascript. I was hoping to get Fancybox working on my website. It actually is already but there's a small problem with dynamic varibles I haven't been able to resolve. I've described the problem in the comment line in the code section. Must be a really simple solution, after all I'm a complete js noob. Note that I've simplified the code by removing parts unnecessary in understanding what I'm after. Code: <html> <head> <script type="text/javascript"> $(document).ready(function() { var title1 = "This is title no 1", title2 = "This is title no 2", title3 = "This is title no 3"; $("a#example_video").click(function() { $.fancybox({ 'title' : 'title' + this.className.replace("video", "") // "title" should equal to "title1", "title2" or "title3" depending on which link a user has clicked. How to accomplish this? </script> </head> <body> <a class="video1" id="example_video" href= .....>Video1</a> <a class="video2" id="example_video" href= .....>Video2</a> <a class="video3" id="example_video" href= .....>Video3</a> </body> </html> Also the only reason for using classnames on anchors is to be able to distinguish between different video clips. Is there a better method I should use? |