MCGRAW
MCGRAW

Reputation: 817

How to add a file extension to a user input field with javascript

Im trying to retrieve a txt doc from server with ajax request. The name of the txt doc depends on a text input on html doc. Basically I want to append .txt to the end of the input field following an onclick event

// JavaScript Document
function getData(){
    var xmlhttp;
    var user=document.getElementById("nameDetails").value;
    var userText = user + ".txt"; //**not the solution
       if (window.XMLHttpRequest)
       {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
       }
       else
       {// code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
       xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
      document.getElementById("userSubmit").innerHTML=xmlhttp.responseText;
      }
    }
    xmlhttp.open("GET","userText",true);
    xmlhttp.send();
}

Upvotes: 0

Views: 116

Answers (1)

poplitea
poplitea

Reputation: 3737

If you want to append .txt to the input field itself, you could try this:

document.getElementById("nameDetails").value = document.getElementById("nameDetails").value + ".txt";

or the short form:

document.getElementById("nameDetails").value += ".txt";

Upvotes: 2

Related Questions