I have a textbox
and put the data like file name (e.g. Mydata.doc
). I want to select the text from the textbox but I don't want to select all text. I just want to select Mydata
exclude extension .doc
. Is there any way to get this.
Asked
Active
Viewed 114 times
-1

kst
- 1,498
- 3
- 19
- 34
-
Its pretty painful, what your looking for is to set 'caret' position, check out: http://stackoverflow.com/questions/512528/set-cursor-position-in-html-textbox All the commenters seem to think he wants to split a string, not what he wants to do I think :s – Josh Mc Apr 22 '13 at 03:27
-
Sorry for duplicate question. – kst Apr 22 '13 at 04:20
5 Answers
2
You can use lastIndexOf()
.
var val = MyData.value;
// base name
var name = val.substr(0, val.lastIndexOf('.'));

Austin Brunkhorst
- 20,704
- 6
- 47
- 61
1
common way to do:
- get the file name like mydata.doc as a string
- find the index of the last "."
- delete the chars from the index of the last "."
- you will get the file name without extention
hope it can help!

Luca Geretti
- 10,206
- 7
- 48
- 58

francis
- 161
- 8
0
This Should work for you
var textboxData = $('.textbox').val();
var newData = textboxData.split('.');
alert(newData[0]);
Here is the LIVE DEMO

Chamara Keragala
- 5,627
- 10
- 40
- 58
0
jquery is javascript. Read the whole string and call the js function split() on the string.

7stud
- 46,922
- 14
- 101
- 127
0
as i tested, i find what we get from input file of html is with a disk path such as "c:\one.jpg".so i update my answer below:
common way to do:
- get the file name like G:\mydata.doc as a string
- find the index of the last "." sIndex
- find the index of the last '\' eIndex
- get the chars from sIndex+1 to eIndex
- you will get the file name without extention
hope it can help!

francis
- 161
- 8