JavaScript - An Easy Question From A Javascript Beginner. How To Add Variable To Itself?
Help me anyone I am completely stuck. If you do this I shall build a shrine in your honour.
In the following code the 'for' loop gets data from the relevant tags of an XML file, then puts them into the 'txt' variable. It then moves onto the next record in the XML file and does the same thing. However, I don't want the contents of txt to be overwritten each time the code loops through each record. Instead I want it to add to txt, then loop around and add the new contents of txt to the previous contents of txt. I tried txt = txt + artist + title + year; but this didn't work. Can anyone explain how to do this? Please note the contents of the variables are strings of text not numbers. Code: x=xmlDoc.getElementsByTagName("CD"); i=0; function displayCD() { for (var i=0;i<x.length;i++) { artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue); txt= "Artist: " + artist + "<br />Title: " + title + "<br />Year: "+ year; } document.getElementById("showCD").innerHTML=txt; } Similar TutorialsWhat I'm trying to test out and learn is the process of a form using the get methode to print out the entered number to another test.htm page 1st code is the form Code: <html> <head> <script type="text/javascript"> </script> </head> <body> <form action="test.htm" method="get"> <font size="1" face="tunga">Enter a number</font> <input type="text" name="number" size="2"> <input type="submit" value="Submit"> </form> </body> </html> the 2nd code is the test.htm page I was hoping the value of var = number would document.write to this page. I was hoping I could do this without using ASP of php. I can see in the URL after the second page loads that the var number dos = the numbered entered but I can not get it to display. Code: <html> <head> </head> <body> <script type="text/javascript"> document.write("The number you entered is " + number); </script> </body> </html> as you can see im a beginner and im trying to learn this. I hope its not a stupid question Thanks John I want to learn javascript and I found codecademy.com which seems like a great way to learn the language. Before the first lesson it says Quote: You should be comfortable with loops, if statements, functions, and objects before attempting this course. Can someone explain what these are or point me in the direction where I can find the answers? I tried searching with google, but I havent found anything that clearly states what exactly the loop, object, etc. is. If I manually write out "2/22/2012 4:00 PM" in TargetDate, I get the correct result for what I want to do (a countdown). But I want the countdown to always be the current day at 4pm. So I tried the code below thats commented, but it is not working. New to javascript, still reading the books, just hoping for some guidance on this. Code: Next Update: <script language="JavaScript"> var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() TargetDate = "2/22/2012 4:00 PM"; //TargetDate = "document.write(month + "/" + day + "/" + year) + 4:00PM"; BackColor = "white"; ForeColor = "red"; CountActive = true; CountStepper = -1; LeadingZero = true; DisplayFormat = "%%H%% H, %%M%% M, %%S%% S"; FinishMessage = "Forecasts posted"; </script> <script language="JavaScript" src="countdown.js"></script> Hey guys, I am working on building a new site and was wondering if I could get some clarity on something with JS, as I am el noobo. I have the basic layout he http://www.temp3.hangnailproductions.com/ Now this is just a mock up and I plan to validate the code, make it more semantic and clean up my CSS quite a bit, but right now I just want to get an idea of what I have to do and something has caught my eye. When the page loads, it takes quite a few seconds for the javascript I have attached to populate a few areas on the screen. At the top of the page there is a random testimonial and the current date. Both are populated from this script: http://www.temp3.hangnailproductions.com/js/master.js I don't know if these areas take a few seconds because my code is horribly wrong, too large, or if this is just normal. Does the array that holds the testimonials hold too much information? I would really appreciate some help. Also, the section that says "This should be a small blurb about the link above." is going to be a snippet of text that changes when a user mouseovers a main link at the top. I think this will also be JS and included in the same "master.js" file - thoughts? Thanks very much. (And I know it is ironic that I am asking for help on a site that sells web design, but Javascript is not something I plan to push, and if need be I would hire someone to help with any in-depth JS. I thought this would just be a simple little project...) Hey guys, I've come here looking for a little help. Probably for a very easy problem to you guys lol. I have a script (this is just a snippet of it): Code: $("#wgo_onlineusers_list").find(":contains('Username')").closest("li").addClass("isOMM"); It basically finds the Username value using the online user list and adds them to a separate special "currently online" list at a different spot on the homepage of the forum. My problem is that if I have a simple username, for example "Big", it adds all usernames with "Big" in it. I know all I need to do is change it so that it catches only the exact username, but I'm having trouble finding a selector (if there is one) that can do this or if it'll be a little more work to fix. Is there any simple fix that is right in front of my eyes for this or do I need to rewrite the function completely? I'm sort of just getting into JS/jQuery and this forum looks like it will be incredibly helpful for me in the future cus of it's activity. Reply With Quote 01-30-2015, 05:17 AM #2 Old Pedant View Profile View Forum Posts Supreme Master coder! Join Date Feb 2009 Posts 28,310 Thanks 82 Thanked 4,754 Times in 4,716 Posts You really need to ask this in the jQuery forum. But I'm with you: I don't see anything in the jQuery docs that supports "equals" instead of "contains" for text values. I just started to learn javascript. can someone please tell me why my script below does not work? Is it not how it should be done? <html> <head> <title> Javascript Navigation </title> <script type="text/javascript"> if(yahoo.onclick()==true) { window.location = 'http://www.yahoo.com'; } </script> </head> <body> <form> <input type="button" name="yahoo" value="Click Here to go Yahoo.com"/> </form> </body> </html> hi guys, l am trying to learn javascript, although certain things seem to be easy to understand, l still have plenty of questions. so l was reading some articles on w3schools and l was an interesting table about operations. http://i1087.photobucket.com/albums/...kesnosense.png if you check above's table you will see at the increment section as well decrement , you will see that in some cases the x and y dont have the same value, now l was just wondering, what is the difference between x=++y and x=y++ ? I'm very very new to javasctipt/html programming and have come upon a stump.Let me explain Code: <form name="for1"> <input type="radio" name="fr1"value="1" > 5 <br> <input type="radio" name="fr1"value="2" > Όσα τα pixels<br> <input type="radio" name="fr1"value="3" > Δεν υπάρχει όριο<br> </form> <input type="button" value="Εκχώρηση Απάντησης" onClick="temp=document.for1.fr1.value"> What i was hoping with this is to store the value of the selected radio in a variable.The variable is declared at the beginning (not shown here).I would appreciate greatly if i'm on the right track to please tell me how to make this work and if possible not create functions and other complicated procedures as it will take me out of my "learning curve".Thanks in advance I am just learning javascript. I am playing around with different html elements and document.write. I have a HTML page with elements h1, h2, h3, and h4. I want to write something (in this example "found it") next to the elements. <head> <script type="text/javascript" language="javascript"> var headerList = getElementsByTagNames('h1,h2,h3,h4'); if (headerList) { document.write("Found it!"); } </script> </head> <body> <h1> </h1> <h2> </h2> <h3> </h3> <h4> </h4> </body> Why doesnt this work? I am assuming my logic is flawed? Hey! Sorry if this is a very remedial question, but I'm trying to make a table, of even and odd numbers organized in two columns (with as many rows as it takes to get through the sequence) and I'm having an issue with my code, obviously - haha. I'm still slightly new.. here is my code Code: document.write('<table>'); for (i=1; i < 50; i++) { document.write("<tr>"); for (j = 0; j < 50; j++) { if (j % 2==0) { document.write('<td>' + j + '</td>'); } else { document.write('<td>' + i + '</td>'); } } } document.write("</tr>); document.write("</table>"); Any help would be greatly appreciated! I don't know much about programming but I can't figure out how this works and I want to implement it on my site. I want to copy the code from this page sdapc.com/billing/pay-online.html and implement something similar. I understand that the form uses post and then goes to form-action.php and the information is verified using the script.js, but how is the information sent anywhere. I mean, I don't see any reference to any script that compiles the data and sends it to the billing department. How do they get the info??? Thanks in advance. P.S. If anyone knows of similar scripts or methods out there of doing this I'm all ears (or eyes) This would just be temporary until full automation is developed. For now, I don't mind entering their info into PayPal myself. This is javascript that turns your current url into a tinyurl. How would I go about getting this to open into a new tab instead of the same tab? Code: javascript:void(location.href='http://tinyurl.com/create.php?url='+encodeURIComponent(location.href)) hey i just started javascript, i have a little syntax question: <HTML> <HEAD> <script type="text/javascript"> function ifPrime(input) { thesqrt = Math.sqrt(input) for(a = 2; a <= thesqrt; a++) { if(input % a == 0) return false; } return true; } function nthPrime(input) { for(i = 2; i <= input; i++) { if( ifPrime(i) ) { document.write(i); document.write("<br />"); } } } </script> </HEAD> <BODY> Enter how many primes <INPUT TYPE=TEXT NAME="userInput" SIZE=6> <INPUT TYPE=BUTTON VALUE="Do it" ONCLICK="value = userInput"> <br /> <br /> <script type="text/javascript"> nthPrime(value) </script> </BODY> </HTML> i would like it post 2 3 5 7 when i type in 10 and press the button. how do I do that? any other programing tips would be appreciated Hi! I am brand new at Javascript. I have finished codecademy and Duckett's javascript book and that is it. I am trying to start coding on my own, without a web interface, and I just cannot figure out how to test my js files. I'm practicing puzzles and made a simple script. It is supposed to take a string as an argument and return the last letter of every word as a "secret message." var decryptA = function(message) { var words = message.split(" ") var secretMsg = [] for (i=0; i < words.length; i++) { secretMsg.push(message[i].slice(-1)) } return secretMsg.join() }; var result1 = decryptA("laugh ride lol hall bozo") console.log(result1) // This should be "hello" var result2 = decryptA("dog polo boo sudd noob smiley ride") console.log(result2) // This should be "goodbye" I think it works, but I cannot figure out how to test it. I made an html file that loads the script: <html> <head><script type="text/javascript" src="testing.js"></script></head> <body><p>Code running...</p></body> </html> and have opened it in firefox and chrome but I just get the "code running" message and nothing else. I have also tried firefox's scratchpad, but nothing happens. I even tried just putting console.log("hello world") into scratchpad and nothing happened. I am getting endlessly frustrated at myself for being unable to run a script. Can anyone offer a concrete way to do this? Background: I am running Ubuntu 12.04 32-bit. Thank you in advance! Cecily Reply With Quote 01-08-2015, 09:33 PM #2 felgall View Profile View Forum Posts Visit Homepage Master Coder Join Date Sep 2005 Location Sydney, Australia Posts 6,745 Thanks 0 Thanked 666 Times in 655 Posts Have you opened the browser console? That's where you are writing the results from the tests. I know it's possible to do this in php, but what would the javascript equivalent be? Code: var scripts = new Array(); scripts[] = "PHP"; scripts[] = "ASP"; scripts[] = "JavaScript"; scripts[] = "HTML"; But this obviously doesn't work. And just to clarify, I mean the ability to add an array whenever I want to in my page instead of having to name the array like... Code: var scripts = new Array(); scripts[0] = "PHP"; scripts[1] = "ASP"; scripts[2] = "JavaScript"; scripts[3] = "HTML"; I'm using it for such as in a form: Code: <input type="text" name="formarray[]"> <input type="text" name="formarray[]"> Thank you in advance. Hey guys, this should be a very easy fix for someone who knows Javascript (which I don't). The following code allows the class "ul.slideshow-nav li" to control the navigation of the slideshow, I want to adjust this script to allow "ul.slideshow-nav li" AND "ul.slideshow-nav2 li" to do this, possible? Code: $slideshow = { context: false, tabs: false, timeout: 8000, fx: 'scrollLeft', slideSpeed: 900, tabSpeed: 900, init: function() { this.context = $('#content-slideshow'); this.tabs = $('ul.slideshow-nav li', this.context); this.tabs.remove(); this.startSlideshow(); }, startSlideshow: function() { $('div.slideshow > ul', $slideshow.context).cycle({ fx: $slideshow.fx, pager: $('ul.slideshow-nav', $slideshow.context), pagerAnchorBuilder: $slideshow.startTabs, befo $slideshow.Tabactive, timeout: $slideshow.timeout, speed: $slideshow.slideSpeed, fastOnEvent: $slideshow.tabSpeed, pauseOnPagerHover: true, pause: true }); }, startTabs: function(i, slide) { return $slideshow.tabs.eq(i); }, Tabactive: function(currentSlide, nextSlide) { var activeTab = $('a[href="#' + nextSlide.id + '"]', $slideshow.context); if(activeTab.length) { $slideshow.tabs.removeClass('on'); activeTab.parent().addClass('on'); } } }; $(function() { $slideshow.init(); }); I am very new to coding, trying to teach myself some javascript. I know this is a very simple code, I just can't seem to nail down what is going wrong. Feeling really stupid right now. Heres the code: Code: <html> <head> <script language="JavaScript"> var choice=confirm("Press ok for black. Press cancel for red."); if (choice==true){ document.bgColor="yellow"; document.fgColor="black"; } else { document.bgColor="red"; document.fgColor="white"; } </script> <body> <p>words</p> </body> </html> If someone could take a second to set me straight, I would really appreciate it. btw: hope I posted all this according to the "rules". I tried hi guys, l am currently studying web technologies on a part time basis, and learning javascript. l got a very useful website, but still l dont understand certain things like this one: Quote: When a function has performed an assignment, it may provide a value that other functions would need, for any reason you judge necessary. When a function produces a value an makes it available to other functions, such a function is said to return a value. To define a function that returns a value, just before the closing curly bracket, type the return keyword followed by the value that the function returns. The value should be a valid and expected type. Because JavaScript is not a type-checking language, meaning it hardly checks anything you are doing, it is your responsibility to make sure that your function returns the right value. Here is a function that returns a value: function rectangleArea(length, height) { var area; area = length * height * 3.14159; return area; } Even if a function returns a value, it can still be called like any other function. If a function returns a value, its name can be assigned to a variable to give a value to, or change the value of, the variable. Here is an example: Code: <Script Language="JavaScript"> function rectangleArea(length, height) { var area; area = length * height * 3.14159; return area; } function displayArea() { var l, h, rectArea; l = 52.05; h = 46.55; rectArea = rectangleArea(l, h); document.write("The area of the is ", rectArea); } </Script> <Script Language="JavaScript"> displayArea(); </Script> what l understand from the code is, l have 2 functions, first function is a return function and has got a value, but where is this value going? l mean what's happing with it? l mean l cant see this return value in the second function, why? l ma also not understand why we need to have 2 functions, why not just 1 ?/?? can someone explain me one by one the lines maybe? thanks I need to change the source of an image but I can't seem to figure out how. I believe that I have the correct syntax, but it does not seem to be working. Maybe I am typing it in wrong or something. I try to test my javascript by typing the script: <strong>javascript:document.getElementById('img1').src='./images/image1.jpg';</strong> into the address bar. Nothing happens. I also try to test it out by putting this on the page, but once again, nothing happens. Code: <script type="text/javascript"><!-- document.getElementById('im1').src="./scripts/'.$upload_image.'"; //--></script> This is what my HTML code looks like: Code: <img src="./images/temp_wizard_picture.gif" alt="" style="padding:4px; background:#fff; border:1px #bbb solid;" id="img1" /> Can somebody please fill me in on what I am doing wrong. Thank you. Hi every one I'm a beginner to java so I have a home work assignment and I'm suppose to create a soda machine thing. What I'm trying to do is make the price of soda * quantity of soda which will equal to the price with tax but I cant figure it out @_@. I know what I'm doing wrong with the "Total" button and the "value" and "quantity" and I've been looking over the internet and playing with it like crazy. So if anyone can help me figure this out that would be great. The code and a pic is on the bottom i hope i gave you guys enough info. This is what im trying to do. |