JavaScript - String Question
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 Similar TutorialsHello, 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. 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 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. 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 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? Hey all, I have a simple example below showing how when I pass in the value of the value attribute of option node, and then use if operator to check whether parameter is a string or not, even though it's a string, it converts it to false boolean and triggers the else statement rather than calling a function: Code: <body> <select> <option value="createMsg">Add Message</option> <option value="removeMsg">Remove Message</option> </select> </body> Code: var menu = { handleMenu : function(callback){ if(callback === "string"){ menu[callback](); } else { console.log("Issue occurred") } }, createMsg : function(){ var content = document.createTextNode("Please give additional information."), heading = document.createElement("h1"); heading.appendChild(content); document.body.appendChild(heading); }, removeMsg : function(){ } } document.getElementsByTagName('select')[0].onchange = function(){ menu.handleMenu(this.value)}; callback should be a string so why is it saying otherwise? Thanks for response I need help with the following code below. It just doesn't turn out okay. Any ideas? Code: function changing() { A = document.exempel.Amount.value B = ("<%#Container.DataItem("Price_2_qty")%>") C = ("<%#Container.DataItem("Price_3_qty")%>") D = ("<%#Container.DataItem("Price_4_qty")%>") E = ("<%#Container.DataItem("Price_5_qty")%>") if (A < B) {document.exempel.Price.value = "<%#Container.DataItem("Price_1")%>"} else if (A = B && (!(A >= C))) {document.exempel.Price.value = "<%#Container.DataItem("Price_2")%>"} else if (A = C && (!(A >= D))) {document.exempel.Price.value = "<%#Container.DataItem("Price_3")%>"} else if (A = D && (!(A >= E))) {document.exempel.Price.value = "<%#Container.DataItem("Price_4")%>"} else if (A >= E) {document.exempel.Price.value = "<%#Container.DataItem("Price_5")%>"} } This is probably easy if I knew Javascript. Can someone show me the code to get the number "800014352" part from the url???? http://testebiz/ebizbtest/ShoppingCa...2/Default.aspx I was looking at this page, and came across "%" used in strange context. It appears that the mod operator can be used in Strings?? http://download.oracle.com/javase/tu...vaOO/enum.html What does this do/ mean? Thanks!!: System.out.printf("Your weight on %s is %f%n", p, p.surfaceWeight(mass)); I am trying to use some string functions with no results. It appear what I'm using them on are not strings. my_string = document.getElementById("link5").onclick; document.write(my_string.lastIndexOf("5")); This gets nothing! And document.write(document.getElementById("link5").onclick.lastIndexOf("5")); gets nothing. BUT if I make a string and put what the onclick is set to with: my_string = "function onclick(event) { reveal(\"5\", \"2\", \"1864\")"; then my_string.lastIndexOf("5") will yield 34. So why is my original my_string = not working? And how can I get it to work? Hey guys. I'm trying to display a grid of data basically, but inside a table and with appropriate closing and beginning of td and tr tags. I'm having a very weird problem. Here is my code: Code: tasks = document.getElementById('tasks'); var i = 0; tasks.innerHTML = <tablebgcolor=silver>"; while (i < 3) { tasks.innerHTML = tasks.innerHTML + "<tr><td>"; tasks.innerHTML = tasks.innerHTML + text[i][0]; tasks.innerHTML = tasks.innerHTML + "</td><td>"; tasks.innerHTML = tasks.innerHTML + text[i][1]; tasks.innerHTML = tasks.innerHTML + "</td><td>"; tasks.innerHTML = tasks.innerHTML + text[i][2]; tasks.innerHTML = tasks.innerHTML + "</td></tr>"; i++; } tasks.innerHTML = tasks.innerHTML + "</table>"; The html just doesn't work when it's in a string. I have to put each "<tr><td>", "</td><td>", </td></tr>", <table bgcolor=silver>", etc. into it's own variable and do it that way. That doesn't seem like it should be happening and I don't think it would be good to do it that way in the long run. Can someone tell me what I'm doing wrong? Hello, I have the following Javascript function: Code: function loadReviews(){ var url='http://search.twitter.com/search.json?callback=?&q=test'; jQuery.getJSON(url,function(json){ jQuery.each(json.results,function(i,review){ jQuery("#divroller_container").append('<div class="reviews" id=item'+i+'>'+review.text+'<br>Reviewed By: '+review.from_user+'</div>'); }); start(30000, 1); }); } I'm trying to use this in a PHP page. I tried to make do an echo ' and then display the Javascript but I'm struggling on how to convert the following line to PHP to that it still works like it should in Javascript: Code: jQuery("#divroller_container").append('<div class="reviews" id=item'+i+'>'+review.text+'<br>Reviewed By: '+review.from_user+'</div>'); I still need the Javascript variables to function the same etc. Any help would be greatly appreciated. Thanks in advance! I'm trying to determine whether a word exists in the URL and if so, change the CSS of a div. I know how to do each task individually, but combining the two is giving me trouble. So far I have: Code: function checkSent(divname) { if(document.URL.indexOf("sent") >= 0) *document.getElementById(success).style.display = 'block'; } var sentornot = checkSent(); // If the URL contains the word, sent, then change the display of the element "success" to block I can successfully determine whether the string exists in the URL by adding an alert, but the Javascript to change the CSS doesn't work. The CSS for the div element "success" is as follows: Code: #success { display: none; } Any help is appreciated! Thanks! Edit: If needed, this is the code I used to test whether the "check URL" portion works: Code: function checkSent(divname) { if(document.URL.indexOf("sent") >= 0) { alert("found"); document.getElementById("success").style.display = 'block'; //this line doesn't work :( } else{ alert("not found"); } } My problem is relates to getting part of the text from a div, then pass this on to a variable which then acts upon it. This is a short summary in detail: http://jsfiddle.net/defencedog/W9Fsw/ Above simulates a Forum like environment (PHPBB3). here user can't insert html in posts thus a customised BBcode ([doc])is made in which is placed the required src attribute, unique to every SCRIBD document. The goal is to get the text within the two [doc] & that will be something like (discarding the two BBCodes) Code: src="http://www.scribd.com/embeds/2942002/content?start_page=1&view_mode=list&access_key=key-sxlnrfvqu5s22kzi5xb" then pass it on as a variable named src, which will then replace the contents to achieve desired output. Notice the two problems 1] how do I select the div upon which the action is to take place I.e that contains [doc]. Remember there occurs other divs as well in a posted message? 2] How do I select the desired text? Plz do giv me some suggestions Hi, I'm coding a program to help people at my office to update the database, and the idea is to copy the content of an excel file and generate an sql from that, but I'm having a bit of trouble splitting the info into different tables: When copying everything the empty cells are read as tabulated, so I made a for that searches for more than one tabulator in a row (or find a \n and a following tabulator character) to split the info into several strings and format the info into a sql table, but I'm stuck he Code: var X=document.getElementById('opc'); var z=X.value.length; for(i=0;i<z;i++){ if(X.charAt(i)==' '&& X.chaAt(i+1)==' '){ char=X.value.substring(0,i-1); break; } } I tried with X.value(i) but I get the same error 'X.charAt is not a function', can someone tell me what am I doing wrong? Thanks. |