Jump to content

How Do I Replace These Pluses With Spaces?


Tim Grollman
 Share

Recommended Posts

Hi, I am passing some user input data from one sheet to another, via<Form Method="Link" ....> The data has spaces in it, which get reinterpreted in the url on arrival as pluses, so I get something like http://mysite.html?Moves=+134U+12345UI can turn this lot into a string by putting it into a text box, and I can truncate it to just give me the +134U+12345U stuff which is what I want, but I can' t see how to replace the Pluses with the spaces that were originally there. All the examples I see use code likedocument.write(str.replace(/microsoft/, "W3Schools"));but this will just writes the revised text to the screen. How do I write the revised text into my textbox?My box is called document.drop_list.movedetails, so I am using things like document.drop_list.movedetails.innerhtml.replace(/+/," ");but it is not happy, because I am over-complicating it. Does someone have a simple solution please?Tim

Link to comment
Share on other sites

You're nearly there, but it depends how you're getting that information from the URL - are you using $_GET["moves"]? However you are, say you take the info from the url and assign it to a variable called data, you'd do it like this:document.drop_list.movedetails.innerhtml = data.replace(/+/," ");Though that's not the best way to reference the element you want - use document.getElementById("id")

Link to comment
Share on other sites

Thanks chibineku, these are the two functions I am now using. Can you help any further please? Thanks, Timfunction fillinbox(){ var locate = window.location //grabs the url document.getElementById("moves").value = locate //puts it into the textbox var text = document.getElementById("moves").value //takes it out again as a string smalltext = delineate(text); //shortens it to get the required substring, which might be 12345U+24D+123U document.getElementById("moves").value = smalltext; //places the substring back into the textbox //document.getElementById("moves").innerhtml = smalltext.replace(/+/," "); // SHOULD take out the pluses and replace with spaces, but isn't working. Comes back blank.}function delineate(str) { theleft = str.indexOf("Moves=+") + 7; theright = str.indexOf("&"); return(str.substring(theleft, theright));}

Link to comment
Share on other sites

Well, there are a few things in the first function that are a little odd. Why do you keep putting the string in the text box? Just do all your manipulation and then put it there - it'll be fast enough that people won't miss it :)function fillinbox(){var locate = window.location //grabs the urlsmalltext = delineate(locate); //shortens it to get the required substring, which might be 12345U+24D+123Udocument.getElementById("moves").innerHTML = smalltext.replace(/[+]/g," "); // SHOULD take out the pluses and replace with spaces, but isn't working. Comes back blank.}function delineate(str) {str +="";var left = str.indexOf("?")+7;var right = str.indexOf("&");return(address.substring(left, right));}str+="" typematches locate to turn it into a string. I altered the str.indexOf("moves=+") to "?" for my test purposes, so swap it back if you need, but if moves is the first variable in the query string, then it works fine as is.

Edited by chibineku
Link to comment
Share on other sites

It will probably be best to use decodeURIComponent, spaces might not be the only thing you have to decode.document.getElementById("moves").innerHTML = decodeURIComponent(smalltext.replace(/\+/g, " "));The plus sign is a special character in regex, it needs to be escaped.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...