QUOTE(Tiff 1998 @ May 7 2017, 07:36 AM)
Hello,
I have a script that copies the anchor text when I click on a link and copies it to a text input.
Hello!
Normally, clicking a link loads a new page, which cancels any script on the first page. Are there more things going on here?
QUOTE
The problem I have is that I need to remove the file extension before it gets copied.
This is what I have:
CODE
$(function () {
$('#rightbox a').on('click', function () {
var text = $('#myfilename');
text.val($(this).text());
$Form.click();
});
});
That's jQuery, which I don't know very well, but isn't "#myfilename" an ID? I think you'd have to get the clicked element's HREF value with
this.href or
this.getAttribute('href');.
Since the HREF value may contain the directory path (and not just the file name) you might split the HREF value by its slashes into an array with
split('/');, where the file name is the last array segment.
Next, split the file name by its period sign(s) into an array, where the last segment will be the file extension. Discard that last segment with the
pop() method.
If the file name doesn't contain any period signs, the array will now only contain a single segment, which is the file name. But if there are period signs (like in "dog.1.jpg") there will be two or more array segments, and you must join them together into a string again (with a period sign between each segment) using
join('.');.
Regex could also be used, and is shorter but usually more tricky.