0

Using JS to get a variable from URL and populate a text box with a first name.

Code below works well, but if the variable has spaces it will insert %20 in the text box. Looking for a way to replace "%20" with " ".

Example: link.com/?fn=Mary%20Ellen

<script type="text/javascript">
function populatefirst() { 
var varSection = window.location.search.substr(1);
var varArray = varSection.split("&");
for(var v=0; v<varArray.length; v++) { 
    var keyValueArray = varArray[v].split("="); 
    if(keyValueArray[0]=="fn") { 
        varValue=keyValueArray[1]; 
        document.getElementById("first_name").value=varValue; 
        break;
        } 
    }
}
</script>

2 Answers 2

3

decodeURIComponent("%20") results in " "

Sign up to request clarification or add additional context in comments.

1 Comment

Where to place? Thanks!!
0
<script type="text/javascript">
function populatefirst() { 
var varSection = window.location.search.substr(1);
var varArray = varSection.split("&");
for(var v=0; v<varArray.length; v++) { 
    var keyValueArray = varArray[v].split("="); 
    if(keyValueArray[0]=="fn") { 
        varValue=keyValueArray[1]; 
        document.getElementById("first_name").value = decodeURIComponent(varValue); 
        break;
        } 
    }
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.