[mdlug] jQuery help

Mark Thuemmel ldaphelp at thuemmel.com
Mon Feb 29 17:49:31 EST 2016


On 02/29/2016 12:39 PM, Bob wrote:
> I have a web form (wordpress plugin) with an image upload field. I need
> to copy the file name to another field so I used this code:
>
> <script type="text/javascript">
> jQuery(document).ready(function($){
> $('#field_g2gofx42').change(function(){
>        var val1 = $("#field_g2gofx42").val();
>        $("#field_jkn3x33").val(val1); }); });
>
> This worked great when I tested from my Linux workstation and a Mac, I
> get this:
> P1010036.JPG
>
> Of course, brain dead windose IE includes the whole local path and now I
> get this:
> C:\fakepath\P1010036.JPG
>
> I tried modifying the code to strip out the "C:\fakepath\" but I either
> get a blank field or Object object.
>
> <script type="text/javascript">
> jQuery(document).ready(function($){
> val1 = $('#field_g2gofx42').val(function(i, v) { //index, current value
>    return v.replace("C:\\fakepath\\", "");
>          $("#field_jkn3x33").val(val1); }); });
> </script>
>
> I spent a good part of yesterday googleing this, found numerous
> different ways to fix this but I'm lost as to how to incorporate them
> into this script.
>
> I'm not much of a JavaScript coder, I muddle along. Any help will be
> greatly appreciated.
>

I'm not a javascripter either, but I'd guess basename is what you want


function basename(str, sep) {
     return str.substr(str.lastIndexOf(sep) + 1);
}


call it like this

basename('file.txt','\');



or maybe this:

function basename(path) {
     return path.replace(/\\/g,'/').replace( /.*\//, '' );
}





More information about the mdlug mailing list