neel
neel

Reputation: 5293

Jquery is not call on button click

i have a jquery for file selection http://jsfiddle.net/parvathy/e8wr5/

please look the fiddle. when i am clicking on the text box, that jquery is worked .. but when clicking on the "browse" button that is not worked.i am using boot strap css for button and input text please help me..

 <input type="text" name="fake_section" id="fake_section" class="form-control" style="float:left;">    
 <button type="button" id="fake_section" class="btn btn-primary" ">Browse</button>


$(document).ready(function () {
$('#fake_section').click(function (e) {
    e.preventDefault();
    $('#file').trigger('click');
});

$('#file').change(function (e) {
    var filename = $(this).val();
    var ext = filename.split('.').pop().toLowerCase();
    if ($.inArray(ext, ['xls', 'xlsx']) == -1) {
        alert('Only add Excel Files!');
    }
    else {
        $('input[name="fake_section"]').val(filename);
    }
});

});

Upvotes: 0

Views: 80

Answers (2)

AFetter
AFetter

Reputation: 3584

Be attention on the ids, your mistake were the same id in two controls. Some IDEs help you with a warning when you have many controls with the same Ids.

<input type="text" name="fake_section" id="fake_section2" class="form-control" style="float:left;">    
 <button type="button" id="fake_section1" class="btn btn-primary" ">Browse</button>


$(document).ready(function () {
$('#fake_section1').click(function (e) {
    e.preventDefault();
    $('#file').trigger('click');
});

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Because you have 2 elements with the id fake_section, use class instead - when you use an id selector it selects the first element with the given id

<input type="text" name="fake_section" class="fake_section form-control" style="float:left;">
<button type="button" class="fake_section btn btn-primary" style="margin-left:10px;">Browse-</button>

then

$('.fake_section').click(function (e) {
    e.preventDefault();
    $('#file').trigger('click');
});

Demo: Fiddle

Upvotes: 3

Related Questions