0

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);
    }
});

});

1
  • 2
    You have duplicate id attribute values. These need to be unique within the document. Commented Feb 5, 2014 at 6:02

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

but i want to use the boot strap class there
@Parvathiiiii you can have multiple classes for an element
Any Idea why I got the Bootstrap styling on your FIDDLE @ArunPJohny, and in Parvathiiiii's not?!?
@dollarvar I added those library references under the External Resources section in the left panel
Ui, nice new knowing about the features in JSFIDDLE, thanks! ;)
0

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');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.