Concatenation of an input value and String variable

10,514

Solution 1

Try

<input type="text" name="text" id="input1" value="ttttttt"/>

JS

<script>
$(document).ready(function(){
    var test = 'blabla' + $('#input1').val() + 'blabla';
    alert(test);
});
</script>

Solution 2

What it seems to me is that either you are missing a jQuery library at the top or this script is not called in the doc ready handler:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
   $(function () {
      var test = 'blabla' + $('#input1').val() + 'blabla';
      alert(test);
   });
</script>

Demo Fiddle

Be sure that your #input1 has a value contained in.

Share:
10,514
Husky
Author by

Husky

Updated on June 23, 2022

Comments

  • Husky
    Husky about 2 years

    I am trying to concatenate the value of the input with the String variable. For instance: (lets say that the input with an id 'input1' has the value of 'tttttt')

    var test = 'blabla' + $('#input1').val() + 'blabla';
    

    So the expected result (at least for me) should be 'blablattttttblabla'. The problem is that it does not insert the value of that input in the middle so the result is 'blablablabla'. Does someone has an idea what I am doing wrong?