Getting the input box name when clicked

197 views Asked by At

In my site I use 10 file upload boxes. I want to get a file upload box name when I click on the box.

That means first upload box, second upload box, third upload box etc...

So if I click on the first upload box then I want to get name of that file upload box.

How can i get the upload button name in ajax function.

This is my ajax code:

$(function(){
    var countfile = 10;
    var strfileid = '';


    for(i=1;i<=countfile;i++){
    var btnUpload=$('#browse'+i);

    var adinfoid=$('#adinfoid').val();
    new AjaxUpload(btnUpload, {
        action: '<?php echo base_url()?>index.php/post/upload_editgalleryimage/'+adinfoid,
        name: 'uploadfile',
        onSubmit: function(file, ext){
        alert(btnUpload.Name);
            var photoplancnt=$('#photoplancnt').val();
            var hidcountimg=$('#hidcountimg').val();
            if(parseInt(hidcountimg)>=parseInt(photoplancnt)){
                $("#photoerror").html('maximum '+photoplancnt +' files are allowed');
                $("#photoerror").css('display','block');
                return false;
            }
            if (! (ext && /^(jpg|png|jpeg|gif|JPG|PNG|JPEG|GIF)$/.test(ext))){
                $("#photoerror").html('Only JPG, PNG, GIF, files are allowed');
                $("#photoerror").css('display','block');
                return false;
            }           
        },
        onComplete: function(file, response){ 

            if(response){                   
                    $(".upload_main_div").html('');
                    $(".upload_main_div").html(response);

                    var insid = $("#hiddengalidnow").val();
                    calltoloadimage(insid);
                    /*$("#galimageicon").attr("src",response);  
                    $("#galimageicon").attr("width",55);
                    $("#galimageicon").attr("height",55);*/
                    //$("#mainimageicon1").attr("src",response);

            }else{
            alert("error");
            }
        }
    }); 
    }
});

It will alert 'browse12' at all time.

Html code:

<?php
for($i=1;$i<=10;$i++){
?>
<input type="button" id="browse<?php echo $i;?>" name ="browse<?php echo $i;?>" class="browse_media" value="Browse">
<?php
}
?>
3

There are 3 answers

4
Sergey On BEST ANSWER

Finally

alert(this._button.name);
1
thecodeparadox On

Generally:

$('input[type=file]').on('click', function() {
 var name = $(this).attr('name'); // or this.name
});

for you case:

btnUpload.attr('name');
0
CamaroSS On

Maybe this will help

$("input[id^=browse]").click(function(){
var strBrowseName = $(this).attr('name'); 
// Or no var, if it has already been defined 
});