I need to split the path entered by the user and grab only certain part of it. For e.g. if the use enters path as:
/content/mypath/myfolder/about/images/abc.jpg
Then I want to display only images/abc.jpg
.
I'm getting
Uncaught Error: Syntax error, unrecognized expression
error at the moment.
Here's my code.
$(document).ready(function(){
$('#getData').click(function(){
imgPath = $('#imgPath').val();
console.log($(imgPath).split('/'));
//console.log(slicedPath);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
Image path: <input type="text" id="imgPath">
<button id="getData">Click</button>
$(imgPath)
will try to locate the element whereimgPath
is selector. As the Path entered by user is not correct selector, it'll throw error. Example, if user entered/content/mypath/myfolder/about/images/abc.jpg
the selector will be$('/content/mypath/myfolder/about/images/abc.jpg')
which is not valid express, thus the error.You may use RegEx to get the image path
The regex will match
images/
followed by any number of characters.match
returns an array, so using[0]
will get the image path.You should use
console.log(imgPath.split("/"))
instead ofconsole.log($(imgPath).split("/"))
.Here
imgPath
is just a variable that stores the input value and not a dom element to be used as$(imgPath)
.I'm assuming to want the last two path values.