0

I wanna change value in span tag element with variable value jquery

I have code like below

<input type="file" name="pdffile" id="fileInput" class="form-control" placeholder="Browse" required>
<span id="pdf" style="display:none;"></span>

And I have jquery like this

$str = $("#fileInput").val();
$splt = $str.split(".");
$('#pdf').text('Your Extension is'.$splt[1]);

Why does it not work?

4
  • Use this code: $('#pdf').innerHTML('Your Extension is'.$splt[1]); Commented Jul 12, 2017 at 3:10
  • 1
    Do you mean the span should be automatically updated every time the input element is changed by the user? @elegent-user - .innerHTML is a property, not a function, but in any case it doesn't exist on jQuery objects. Commented Jul 12, 2017 at 3:24
  • You are concatenating javascript like it's php. At least that's my first impression. See my answer for more details. Commented Jul 12, 2017 at 3:35
  • @nnnnnn, .html is the correct method that I should have written. I missed. Thanks for pointing out the mistake. Commented Jul 12, 2017 at 5:36

4 Answers 4

1
$('#fileInput').change(function() {
        $str = $("#fileInput").val();
        $splt = $str.split(".");
        $('#pdf').html('Your Extension is ' + $splt[1]);
});
Sign up to request clarification or add additional context in comments.

Comments

0

You are concatenating your variable wrong. You need to add a + and put the dot within the parenthesis. Also the display: none hides the result from view.

JSFiddle

html

<input type="file" name="pdffile" id="fileInput" class="form-control" placeholder="Browse" required>
<span id="pdf"></span>

js/jQuery

$("#fileInput").change(function(){
$str = $("#fileInput").val();
$splt = $str.split(".");
$('#pdf').text('Your Extension is .'+$splt[1]);
});

3 Comments

On the click event, the is no filename yet in the field... Or it is the previous filename.
@Louys Patrice Bessette Hence the post scriptum.
@Louys Patrice Bessette I wouldn't say wrong. The question doesn't specify when the function needs to be run. But I suppose it does makes the answer a bit clearer. So I changed it :).
0

In case of a filename containing dots in the filename part, like my.trip.pictures.jpg...
I suggest using regular expression to be sure to catch the last dot and the extention.

$("#fileInput").change(function() {
  
  var ext = $(this).val().match(/(.+)(\..+)/)[2];

  $('#pdf').show().text('Your Extension is ' + ext);

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" name="pdffile" id="fileInput" class="form-control" placeholder="Browse" required>
<span id="pdf" style="display:none;"></span>

Comments

0

$("#fileInput").change(function() {
  var qwe = $(this)[0].files;

  console.log(qwe[0].name.split('.').pop())// another option to get extension name
  $('#pdf').show().text('Your Extension is ' + qwe[0].name.slice(-3));

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" name="pdffile" id="fileInput" class="form-control" placeholder="Browse" required>
<span id="pdf" style="display:none;"></span>

Try this way

1 Comment

Using slice(-3) will only retrieve the last three characters. This would cause issues with image formats like jpeg.

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.