JavaScript - Beginner Needs Javascript Help
Hi people I'm hoping someone can help me.
I have NO knowledge of Javascript but I'm not afraid of code and happy to look at it and work out what is doing what. I need someone to provide me with a small sample of code to get me started on this task. I need a webpage with a number of drop down menus. Depending on what the user selects in these menus, depends on what sentence or paragraph is then displayed in a box. For example you might have a page dedicated to food. So the user would select egg in the first drop down and the box will change to display "you can't make much with that on it's own".# You'll then select flower in the second and it'll adjust to a paragraph maybe explaining where you could go next with that. In my page I need 6 drop down menus with about 8 choices in each menu (nothing related to food). Not sure if I would need to tie this in with a database or if I could just make do with the code on the page. As I said I'm hoping someone can just kick me off with something do I can get a rough idea of how I would go about coding it. Maybe an example contain 3 drop down menus with 2 choices on each and a few possible sentences or paragraphs that it could generate in a box. If not then any advice would be really appreciated as I said I'm a complete newbie and I've never even looked at Javascript code to my knowledge. Many thanks Similar Tutorialsis this code right its supposed to be a code that prompts the user for a number less than 100 and outputs only odd numbers less than or equal to the number entered by the user.for example if a user eneters the number 10 you should display 1,3,5,7,9 to the browser window?...im not sure if i have done it right..i gave it a go though! var input = prompt("Enter a number less than 100:", "10"); if( input <= 100){ nbsp; for (var i = 1, i <= input, i++2){ nbsp; document.write(i", "); nbsp; } }else{ nbsp; alert(input + " is not less than 100!"); } 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. I have to design a javascript program to capture five different numbers (monetary values), and save it in memory. Then another one that converts that value to, let's say, Euros, and display interest and commisions (which are values I'll have to add later, but the actual coding for the calculation is what's confusing to me. Let's just say, take 10% out of the initial value for interest, and 15% for commission). I would appreciate any help
I dont know the first thing about java script but I also didnt know the first thing about html and css but someone showed me a few basic things to gt started and Ive excelled in both using dreamweaver. I just needed the basics and then I was able to work through it. Just like that I need help starting out with Java Script. Basically, Ive made a website and now I am trying to put in a slider(it is a photo slider which has a box drop down on the right with a description when it changes photos.) I was told to make a slider.css file in the css folder and a slider.js file in the java script folder. I coded everything correctly in the source code page to get the slider up and running. But it wont work. Am I supposed to have a Java script program downloaded or something? Please help if you know what Im talking about Ive hit a road block and if you can walk me through this process I would really appreciate it and I can continue with my project. Thanks Hi, I am fairly new at Javascript but have some ability - though I can't figure this one out. I need a JavaScipt "IF Statement" that validates that a Text Field is in the format of 2 Letters then 2 Numbers. The field also has to contain a total of 4 characters no matter what. For example, every entry for that Text Field has to be in the following format: AB34 I would sincerely appreciate anyones help! What 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 Hello, I very new to this language and I still feel like I'm not grasping it... But, anyways, I'm doing a thing for school and I have to let the user input a string. I need to save the string and turn it into an array. For every word that is less than five letters I put "little" at the beginning of the word and for every word that is more than five letters I put "big" at the end of each word. Then I need to return the new string into output. I think I wrote the code really incorrectly, so any tips/advice would be much appreciated! Also, I don't understand adding user input into a code if that makes sense. The tutorials/lessons I've been looking at all say to put information into an array first and then mess with it. But, what if you don't have information in the array until the user puts it in? And once they enter it, then you mess with what they entered. I can't seem to get how to do that. Thank you for your time Code: function texter(newText){ var oldHTML = document.getElementById('outputPrompt').innerHTML; document.getElementById('outputPrompt').innerHTML = newText+"<br />"+oldHTML; console.log(newText); } function menuTwo(){ var userInput = document.getElementById('input').value; var correctedInputArray = userInput.toLowerCase().split(" "); var mainTwo = new Array([""]); for(var i=0; i<correctedInputArray.length; i++){ var thisWord = correctedInputArray[i]; var lessFive = 5; var moreFive = 6; var restOfWord; if(lessFive<5){ mainTwo[i]=thisWord+"-little"; }else if(moreFive>6){ mainTwo[i]="big-"+thisWord; } else{ restOfWord = thisWord.substr(1, thisWord.length-1); } } output = mainTwo.join(" "); texter(output); } </script> </head> <body> <h1>Document</h1> <input type='text' id='input' /> <input type='button' onclick='menuTwo()' value='submit'/> <p id='outputPrompt'>Please enter 1,2,3 or exit only</b> </p> </body> </html> Hi, I am working on a simple javascript craps game program. I need some advice since it won't display who the winner is, keep tally of who wins/loses, and the number of total games played. After using the error console there's an error with document.forms[0].thrower.value not being defined. Can anyone help me with this? PHP Code: <html> <head> <title> JavaScript Craps Game</title> <script type="text/javascript"> <!-- var n,die_1,die_2,total,h_won,c_won, flag, point,winner; function get_num() { var max = 6; var number=Math.random()*max + 1; var result=Math.floor(number); return result; } function roll_dice() { die_1 = get_num(); die_2 = get_num(); total= die_1 + die_2; // Insert the results of the dice into the appropriate fields document.getElementById("die1").innerHTML= die_1; document.getElementById("die2").innerHTML= die_2; document.getElementById("total").innerHTML= total; //document.forms[0].die1.value = die_1; //document.forms[0].die2.value = die_2; //document.forms[0].tot.value = total; //Subtracting 0 from these values forces them to be typed as numbers if (flag){ //This means we rolled something other than 2,3,7,11, or 12 so we have a point var th = document.forms[0].thrower.value; if (total == point) { //X wins winner = "x"; calculate_winnings(th,winner); flag = 0; //document.forms[0].flag.value="0"; game_count(); } else if (total == 7){ //Y wins winner = "y"; calculate_winnings(th,winner); flag = 0; //document.forms[0].flag.value="0"; game_count(); } } else{ var thwr = document.forms[0].thrower.value; if (total == 7 || total == 11) { //X wins document.getElementById("winner").innerHTML= "X Wins!"; //document.forms[0].winner.value= "X Wins!"; winner = "x"; calculate_winnings(thwr,winner); game_count(); } else if (total == 2 || total == 3 || total == 12){ //document.forms[0].winner.value= "Y Wins!"; document.getElementById("winner").innerHTML= "Y Wins!"; winner = "y"; calculate_winnings(thwr,winner); game_count(); } else { point = total; document.getElementById("winner").innerHTML= "Waiting for a 7 or a " + point; // document.forms[0].winner.value="Waiting for a 7 or a " + point; flag = 1; //document.forms[0].flag.value="1"; } } } function game_count() { //for keeping track of games if (n){ n = n + 1; } else { n = 1; } document.forms[0].totalgames.value = n; //return n; } function calculate_winnings(thrower,winner) { var button = document.forms[0].thrower.checked; if (button) { if (winner == 'x') { //computer was thrower and X won //document.forms[0].winner.value="Computer Wins!"; document.getElementById("winner").innerHTML= "Computer Wins."; add_to_computer_win(); } else { //computer was thrower and Y won //document.forms[0].winner.value="You win!"; document.getElementById("winner").innerHTML= "You win!"; add_to_human_win(); } } else { if (winner == 'x') { //human was thrower and X won //document.forms[0].winner.value="You Win!"; document.getElementById("winner").innerHTML= "You Win!"; add_to_human_win(); } else { //human was thrower and Y won //document.forms[0].winner.value="Computer Wins!"; document.getElementById("winner").innerHTML= "Computer Wins."; add_to_computer_win(); } } thrower = 0; } function add_to_human_win(){ if (h_won){ h_won = h_won + 1; } else { h_won = 1; } document.forms[0].human_won.value=h_won; } function add_to_computer_win(){ if (c_won){ c_won = c_won + 1; } else { c_won = 1; } document.forms[0].computer_won.value=c_won; } --> </script> </head> <body> <h1> <center> Craps Game </center></h1> <hr> <form> <table border="1"> <tr> <td width="45%" align="center"> <center><b><font size="4">Play!</font></b></center> <p><input type="button" name="roll" value="Roll Dice!" onclick="roll_dice()"></p> <table border="1"> <tr> <td>Die #1</td><td>Die #2</td><td>Total</td> </tr> <tr> <td><div id="die1"></div></td> <td><div id="die2"></div></td> <td><div id="total"></div></td> </tr> </table> <p>Result of roll: <div id="winner"></div></p> <!--<input type="text" size="30" name="winner" value=""></p>--> <p> </p> </td> <td width="35%"> <table border="1" cellspacing="7"> <tr> <th colspan="2" ><font size="4">Statistics:</font><br> <input type="text" size="2" name="totalgames" value="0"> total games played</th> </tr> <tr> <td align="center">Your wins</td><td>Computer wins</td> </tr> <tr> <td align="center"><input type="text" size="2" name="human_won" value="0"></td> <td align="center"><input type="text" size="2" name="computer_won" value="0"></td> </tr> </table> </td><td width="20%" align="center"> This will clear your statistics and start a new game<br> <input type="submit" name="startover" value="New Game"> </td></tr></table> <hr> <h3><a name="Help">Help</a></h3> <pre> The game of craps is a dice game played by two players, You and The House. First you toss the pair of dice. If the sum of the dice is 7 or 11, you win the game. If the sum is 2, 3, or 12, the house wins. Otherwise, the sum is designated as the "point," to be matched by another toss. So if neither player has won on the first toss, then the dice are tossed repeatedly until either the point or a 7 comes up. If a 7 comes up first, the house wins. Otherwise, you win when the point comes up. </body> </html> 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; } 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. Hi there, im wondering if you can help me i need help in finding javascript resources to build my skills and knowledge of Jscript because im a beginner. Please help I will start off by saying yes this is homework but I only want help. I'm trying to make the content show up in the read only boxes but I can't figure out where I'm going wrong. My test page can be found at http://sidewalk-cafe.com/test/maddox-hw2a.html. It's small. Any help would be appreciated. Code: function orderForm() { var formDaysStaying = document.getElementById("daysStaying").value; var formSeason = document.getElementById("season").value; var formPropertyType = document.getElementById("propertyType").value; document.getElementById("FEE").value = "$" + today.toFixed(2); document.getElementById("totalCost").value = "$" + today.toFixed(2); document.getElementById("today").value = "$" + today.toFixed(2); document.getElementById("due").value = "$" + due.toFixed(2); } function formTotalCost(form) { const FEE = 55; var totalCost = (formPropertyType * formSeason) * formDaysStaying; var today = totalCost * .10; var due = (totalCost + FEE) - today; if (document.getElementById("daysStaying").value == "" || isNaN(document.getElementById("daysStaying").value)) alert("Please provide the number of days you will be staying."); // Submit the order to the server form.submit(); } I am very new to JavaScript and I am currently following the Eloquent JavaScript book online. On one of the exercises, we are told to make a function that returns the range of the inputs in an array. I tried this code below but it doesn't seem to work. The problem seems to be in the "If" function because if I change the condition to (start < end) it seems to work. Can someone please explain to me why this is? function range (start, end) { var j = []; if (start > end) { for (i = start; i <= end; i++) { j.push(i); } } else { for (i = start; i >= end; i--) { j.push(i); } } return j; } console.log(range(10, 2)); console.log(range(2, 10)); Reply With Quote 01-26-2015, 08:49 PM #2 Labrar View Profile View Forum Posts New Coder Join Date Jun 2008 Posts 65 Thanks 0 Thanked 12 Times in 12 Posts Why you do not use something like this var range = function(start, end, step) { var range = []; var typeofStart = typeof start; var typeofEnd = typeof end; if (step === 0) { throw TypeError("Step cannot be zero."); } if (typeofStart == "undefined" || typeofEnd == "undefined") { throw TypeError("Must pass start and end arguments."); } else if (typeofStart != typeofEnd) { throw TypeError("Start and end arguments must be of same type."); } typeof step == "undefined" && (step = 1); if (end < start) { step = -step; } if (typeofStart == "number") { while (step > 0 ? end >= start : end <= start) { range.push(start); start += step; } } else if (typeofStart == "string") { if (start.length != 1 || end.length != 1) { throw TypeError("Only strings with one character are supported."); } start = start.charCodeAt(0); end = end.charCodeAt(0); while (step > 0 ? end >= start : end <= start) { range.push(String.fromCharCode(start)); start += step; } } else { throw TypeError("Only string and number types are supported"); } return range; } Thats pretty similar to php range Reply With Quote 01-26-2015, 09:25 PM #3 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 Ignore Labrar's ridiculously complicated answer to a very simple problem. Instead look CAREFULLY at this code: Code: if (start > end) { for (i = start; i <= end; i++) { Let's use your first example, where you call range(10, 2) So start is 10. And end is 2. So you get to if ( start > end ) and JavaScript executes that as if ( 10 > 2 ) Indeed, 10 *IS* greater than 2, yes? SO you the go execute the NEXT line: for (i = start; i <= end; i++) Which, of course, JavaScript then executes as for (i = 10; i <= 2; i++) But then 10 is *IMMEDIATELY* <= 2. So the for loop NEVER RUNS. All you need to do to fix your code is change Code: if (start > end) { into Code: if (start < end) { PRESTO! ********** The tiniest bit of debugging using your JavaScript debugger would have found this. But also just learning to THINK like the computer would help. Actually run the code in your head, using your example numbers, and you'd see where you went wrong. hello! i have a problem: i want to change the height of my side bar as long as the text in the center of page long, it means when I have for ex 3 news in a page the side bar which have a background color must be increase to fit the long of text(3 news). i think it will do by javascript but i'm not sure! i my question isn't clear please tell me to explane more i really need it, 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. Hi all, this is my first attempt at JavaScript code. I'm trying to write code for an Adobe Acrobat form. Here is my situation: I have several boxes, all containing numbers such as 2014, 08, and 000000000. I want to concatenate them all into one long number like 080000000002014. I've looked at a ton of sample code, but nothing I try seems to work. Any help is greatly appreciated.
var suitArray = ["Hearts","Diamonds","Clubs","Spades"]; var faceArray = ["Ace", - Pastebin.com Doing a school assignment and Im totally stumped, google isnt any help nor is any of these textbooks. I need to tally up the playerhands and dealer hands and display the winner. I can get it to display the value of the current card. eg 7 of diamonds = 7 points but I cannot get it to tally up all the cards dealt. Hey guys, Thanks for taking the time to read my post. I am not new to programming or scripting, but I am completely new to javascript and web-based code. Hopefully you guys could help me understand what is going on and maybe help me figure out what I am trying to do. What I am trying to do: -There is an element that has a value <td class="flashField" onmouseover="clearAnswer()"> <div id="flashAnswer" onmouseover="clearAnswer()">answer string here</div> </td> -There is a text box that needs to have the answer above given to it <td class="flashField"> <input type="text" tabindex="1" onkeypress="clearAnswer()" name="q" maxlength="256" size="50"> </td> -There is a next button that needs to be executed <td align="right"> <input type="image" tabindex="2" alt="Next" src="http://website.com/images/next.gif"> </td> So what I am trying to do is create a script through Greasemonkey that will automatically do these things. I am starting small and trying to just get the value of flashAnswer to appear as an alert on the screen by using this code: var answer = document.getElementById('flashAnswer'); alert(answer); But the problem is it posts some weird value ([object XrayWrapper [object HTMLDivElement]]) that most definitely is not "answer string here" and is a bit above me at this point. Any ideas? Any help would be great guys! Thanks! -ellosiph I'm trying to get this simple (not to me) ascii animation going and firebug is giving me an error saying "document.getElementById("frameArea") is null. I've got an element named frameArea and I wonder if it's buried too deep. I don't really understand this code which I admit is a big part of my problem. I have seen what it is supposed to do, which is play a simple animation. Will someone please help me decipher what this code is saying? Code: <html> <head> <title>Animation</title> <script type="text/javaScript"> function playAnimation() { frameStr = document.getElementById("frameArea").value; if(frameStr.indexOf("\r\n") != -1) { frameSeq = frameStr.split("=====\r\n"); } else { frameSeq = frameStr.split("=====\n"); } currentFrame = 0; showNextFrame(); } function showNextFrame() { document.getElementById("displayArea").value = frameSeq[currentFrame]; currentFrame = (currentFrame+1)% frameSeq.length; timer = setTimeout("showNextFrame();", 250); } </script> </head> <body> <div style="text-align:left"> <h1>My Animation</h1> <hr /> <form name="animationForm"> <table> <tr><td align="center"><b>Enter frames below separated by "====="</b> <td> <td align="center"> <tr><td><textarea name="frameArea" rows=12 cols=35 wrap="virtual"></textarea> </td> <td align="center"> <br /> <input type="button" value="Play --->" onclick="playAnimation()"> </td> <td><textarea name="displayArea" rows=12 cols=35 wrap="virtual"></textarea> </td> </tr> </table> </form> </div> </body> </html> Hi there! Here's my problems: The problem 1 is about making triangle with "*", but i must not use <center> so the triangle must be on the left side.. Here's my code: var x = parseInt(prompt("Your value","")); var A= ""; var B=[" "]; for (i=0; i<x; i++) B[i+1]=B[i]+" "; for (i=0; i<x; i++) { A = A+"*"; document.write("<br>" + B[x-i] + A) } How can i make this code more basic? The problem 2 is about taking random different elements from Array. Here's code: var items = Array(0,1,2,3,4,5,6,7,8,9), a; document.write("Secilen elemanlar: "); for (i=0; i<5; i++) { a= Math.floor(Math.random()*items.length); document.write(items[a]); items.splice(a,1); } In that problem how can i do the same thing without splice. And the problem 3 is about number triangle like this 1 232 34543 I couldn't write any code,and cane you give some advices like "what methods i must use"? Reply With Quote 02-04-2015, 07:34 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 splice is about the only command there that you should consider using. Yuo are misusing parseInt() where you should be using Number() and the prompt() and document.write() should never be used at all - they became obsolete many years ago. |