JavaScript - Splitting A Text String Question
Hello,
How would I seperate a text string such that it would appear on seperate lines ie. Initial Input: ------------------------------------------- StrMsg = "This is an example of a string that will appear on seperate lines" "Hoping that this fully works, there will be no errors and all will be well" "Thank you to the experts and pros on this forum who are here to help" "This is the last line of text." Expected Outcome: ------------------------------------------- This is an example of a string that will appear on seperate lines Hoping that this fully works, there will be no errors and all will be well Thank you to the experts and pros on this forum who are here to help This is the last line of text. Much thanks and appreciation for all your help J Similar TutorialsWhat would the best be to split the following string: "only(a-z,A-Z,1-9)" I want to split it into a array to look like this: array(only, a-z, A-Z, 1-9); Hello, I need your help. Let's say for example if I have the following file number: 2011-1234567 and if the value of the variable is already the same (ie. var fileNo = 2011-1234567) how can I go about automatically adding a number at the end? ex.1.) var fileNo = "2011-1234567" if (fileNo already exists) { fileNo = 2011-1234567-2 } ex.2.) var fileNo = "2011-1234567-2" if (fileNo already exists) { fileNo = 2011-1234567-3 } ect. Much thanks and appreciation for all your help. Jay Hello, I need your help. How could I write a functional if statement such that it would check against a series of various word strings seperated by commas ie. function check(y) { //y = "FOR INPUT" var string_to_check = FOR INPUT, FOR ACTION, FOR APPROVAL if (y isin string_to_check) { return 1 } } Thanks for everyones help. J Hello, I need your help. For one reason or another, I have a textarea with text in it, however, when I call it back and store it in var, it seems to strip all the spaces. [ID = "COMMENTS"] Ie. of what's in the textarea: ---------------------------------------------- Apples are red Oranges are orange Pears are green Bananas are yellow var comments = document.getElementById("comments").value returns: Apples are red Oranges are orange Pears are green Bananas are yellow How could this be re-written such that the spaces in between the text aren't ignored? Much thanks and appreciation for everyones help. Cheers J I have a Number as a String. It goes from 1 to 999 but I need to set the format so that it has leading Zeros. So 1 is 001 and 2 is 002 and 99 is 099. Is there a way to do this in JavaScript? Thanks, ~Dave Hi, I'm fairly new to javascript as well as brand new to these forums. I have a question that I'm sure has a simple answer but I either can't get my head around it or I'm just too unfamiliar with string methods. I'm trying to replace all instances of "&" with its alt code "&" in a string. Below is my code.. Code: Match = subText.indexOf("&"); while (Match != -1) { subText = subText.replace( "&", "&" ) Match = subText.indexOf("&"); } The problem here is that an ampersand will always exist in the string if there was one originally and I get an infinite loop. Would anyone know of a way around this? Thank you. I want to Count the number of occurrences of character 't' in a string entered by the user (via prompt) and write the result to the web page. For example, if the user enters "Working with strings", you should write a message saying that the string contains 't' 2 times. I know it has something to do with charAt. Im just playing around with it trying to figure it out and got Code: var str = prompt(); document.write(str.charAt(1)); That doesnt count anything up obviously. i need to use a loop but im not sure how to apply a loop and charAt. Function findsometing() ( var myString = 'results' + '<BR>' for (var initiator, test condition,incrementor.) { if (.....) { myString ='some quote'; } else if (.....) { myString += something + '<br>' } else if (....... ) { myString += something + '<br>' } else { myString += ''; } } displayMessage(myString) ) I am a complete newbie to javascript and programming, so excuse any faux pas, but I want to check the myString variable after all the loops in the for loop statement have been completed. For example if nothing was added to the var myString after the completeion of the for loop, i want to display a message that no results found. i tried various attempts of an if statement along lines of } } if (myString = 'results') ....i also tried if myString= ('results' + '<BR>') { displayMessage('no results found') } else { displayMessage(myString) } ) Any pointers as to where i am going wrong? I am just starting to learn javascript and was hoping someone might be able to answer my question: var months = "April Showers"; document.write('<p><img src="ad11.jpg"); document.write(" alt="); document.write(months); document.write(">"); document.write(months); How come when I display my output to a browser without the presence of the ad11.jpg file, only the first word (April) in the text string "April Showers" is dispayed where the .jpg file is suppose to be...but the last line displays the entire text string. Hello, I need your help. I would like to insert a few blank spaces in a text string: eg. string = 123456(space)(space)(space)7891011 How do you do this? Thanks a bunch Jay I need to change one letter in a text string, but near as I can figure out, I can't just do this: Code: TextString[8] = "X"; Is that possible, or do I have to break the string and rebuild it into a second string? Hi there, I want to prompt a user to enter their name, if the user entry includes numbers I would like to prompt them to re-enter their name using only text. I have tried: Code: var userName = prompt ("Please enter your name", " "); if (userName == null || !isNaN(userName)) { prompt ("Please enter a valid name, and do not include any numbers in this field"); userNamm.focus(); } else { document.write("Welcome back " +userName); } It isn't working, I would like to monitor the user entry for only text values, can anyone help me to clean this up? I'm trying to figure out how to split a url variable... I tried these two ways, with an alert to see my result, but I'm not getting anything. Suggestions? Code: function() { myURL = "http://www.test.com/folder1/folder2/someone" var partsA = myURL.split("//"); var parts = partsA[1].split("/"); alert(parts[2]); } Code: function() { myURL = "http://www.test.com/folder1/folder2/someone" var parts = myURL.split('/'); var result = parts[parts.length-2] + '/' + parts[parts.length-1]; alert(result); } I am really new to JavaScript, and was following a tutorial that was using everything inline (probaby as its less confusing for the noob). Now I want to split it off into a script area, like my CSS has been. I have: Code: <SPAN onclick="document.getElementById('glance').style.display='block';document.getElementById('front').style.display='none';"> Is something like this valid (this is a guess BTW)? Code: <SCRIPT type="text/javascript"> function getElementById('glance') { .style.display='block'; } function getElementById('front') { .style.display='none'; } </SCRIPT> <SPAN onclick="document.getElementById('glance'); document.getElementById('front')"> ... </SPAN> ... or should function be document in the script area and document not be present in the SPAN attribute? ... Better yet, is there a way to put both the 'glance' and 'front' together like this: Code: <SCRIPT type="text/javascript"> function getElementById ('glance') { .style.display='block'; getElementById ('front') { .style.display='none'; } } </SCRIPT> Alright, so I'll be honest, I HAVE NO IDEA ABOUT JAVASCRIPT! So, basically, I want, when the button is clicked, for this to grab some levels from a game and do some calculations. Simple. Well... Not for me. Here is the code I have right now: Code: <script language="JavaScript" type="text/javascript"> function GrabTheStats(rsusername){ var GrabStats = ""http://hiscore.runescape.com/index_lite.ws?player="" + rsusername; var GrabStats_array = GrabStats.split(","); attLvl = expToLevel(GrabStats_array[0],false); strLvl = expToLevel(GrabStats_array[1],false); defLvl = expToLevelGrabStats_array[2],false); hpLvl = expToLevel(GrabStats_array[3],true); magLvl = expToLevel(GrabStats_array[4],false); ranLvl = expToLevel(GrabStats_array[5],false); praLvl = expToLevel(GrabStats_array[6],false); sumLvl = expToLevel(GrabStats_array[7],false); calcResult = calcCombat(attLvl,strLvl,defLvl,hpLvl,magLvl,ranLvl,praLvl,sumLvl); document.getElementById('outputlocation').value = calcResult[0] + "/" + calcResult[1]; } </script> Code: <input type="text" name="outputlocation" id="outputlocation"/> <input type="button" onclick="GrabTheStats("zezima");" value="CLICK ME" /> Could someone help me to get this working? The calcCombat calculates things bases on the levels it grabs and produces an array of results, calcResult[0] and [1] being the two results I want right now. An example output will hopefully be "95/98". Please don't criticize, since this is for 'crappy old RuneScape'. It really doesn't help. Thanks! I am trying to split the (id) values between seperate forms and the submit buttons are not located between the form tags. Note: ->The 3 forms are located at the top of page ->the (3) Submit buttons are in a query and are outside the form tags. ->In this case it does not make sense to have the form duplicating itself in the sql_query. need the correct code for the 3 script functions var form1, form2, form3 then i need the correct function submit code something like -> onClick='Split(this.form1,1)' Also: To keep this simple could the 3 scripts be combined into one? Code: <!-- SPLIT VALUES BETWEEN SEPERATE FORMS --> <script type="text/javascript"> function Add(sel,first) { if (sel.selectedIndex == 0 ) return; var temp = sel.id.split(","); sel.form1["F"+first].value = temp[0]; sel.form1["F"+(first+1)].value = temp[1]; } </script> <script type="text/javascript"> function Edit(sel,first) { if (sel.selectedIndex == 0 ) return; var temp = sel.id.split(","); sel.form2["F"+first].value = temp[0]; sel.form2["F"+(first+1)].value = temp[1]; } </script> <script type="text/javascript"> function Delete(sel,first) { if (sel.selectedIndex == 0 ) return; var temp = sel.id.split(","); sel.form3"F"+first].value = temp[0]; sel.form3["F"+(first+1)].value = temp[1]; } </script> <form method='post' name='form1' action='record_add.php'> <input type='hidden' id='F1' name='t101' value='' > <input type='hidden' id='F2' name='t102' value='' > </form> <form method='post' name='form2' action='record_edit.php'> <input type='hidden' id='F3' name='t103' value='' > <input type='hidden' id='F4' name='t104' value='' > </form> <form method='post' name='form3' action='record_delete.php'> <input type='hidden' id='F5' name='t103' value='' > <input type='hidden' id='F6' name='t106' value='' > </form> <!-- NOTE: THE SUBMIT BUTTONS ARE IN A QUERY OUTSIDE THE FORM TAGS AND NEED TO TARGET THE PROPER FORM1,2,3 onClick --> <button type='submit' id='$mytable,$columns[Field]' onClick='Split(this.form1,1)'> <button type='submit' id='$mytable,$columns[Field]' onClick='Split(this.form2,3)'> <button type='submit' id='$mytable,$columns[Field]' onClick='Split(this.form3,5)'> I want the inserted text to be under the droplist not on top of it! <html> <body> <script type="text/javascript"> function showSelected(val) { document.getElementById('selectedResult').innerHTML="<a href='mailto:" + val + "'>" + val + "</a><p><font size=3></font> </p>" } </script> <div id='selectedResult'></div> <!-- This sets up the drop down list of names --> <select name='test' onChange='showSelected(this.value)' ID=Select1> <option value='user.1@email.net'>First User</option> <option value='user.2@email.net'>Second User</option> </select> </body> </html> Hello im trying to update a text area value with edited code with javascript, i dont know how to and im asking if you guys know
|