0

Using split method I want to break url and want only image name in input fild after selecting file from folder. but it showing whole path c:\fackpath\xyz.png

<html>
<head>
    <script type="text/javascript">
        function alertFilename()
        {
          var thefile = document.getElementById('thefile').value;
         var x = thefile.toString()
         var y = x.split("\")[0];

         var y = document.getElementById('x').value = thefile;



        }
    </script>
</head>
<body>
    <form>
        <input type="file" style ="width:90px" id="thefile" onchange="alertFilename()" />
        <input type="text" id ="x" value=""  />
        <p id ="demo"></p>
    </form>
</body>

here is I attache plunker link

5
  • 1
    did you try split("\\") ? Commented Jan 15, 2017 at 20:17
  • No, I did not try . Commented Jan 15, 2017 at 20:18
  • thank you so much it works. Commented Jan 15, 2017 at 20:20
  • I didn't understand concept of "\\" Commented Jan 15, 2017 at 20:21
  • Your code had a syntax error, it is like this that ` \ ` is an escape character, where your code escaped the " character, which ment, you never really closed the string. By writing ` \\ ` you are actually looking for ` \ ` ;) (also in the comments it seems) Commented Jan 15, 2017 at 20:22

1 Answer 1

1

I think you just need to write double backslash for right split, because backslash is a special char.

var y = x.split("\\")[0];
Sign up to request clarification or add additional context in comments.

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.