-1

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.

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 Answers5

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:

  1. get the file name like mydata.doc as a string
  2. find the index of the last "."
  3. delete the chars from the index of the last "."
  4. 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:

  1. get the file name like G:\mydata.doc as a string
  2. find the index of the last "." sIndex
  3. find the index of the last '\' eIndex
  4. get the chars from sIndex+1 to eIndex
  5. you will get the file name without extention

my test, and it does help hope it can help!

enter image description here

francis
  • 161
  • 8