1

i want to append a text into an input value using jquery, i dont want to use val() :

The code :

        <span>L'objectif de ce cours est d'introduire</span>
        <script>
            $(document).ready(function(){
                var tb = $("span").text();
                console.log(tb); // L'objectif de ce cours est d'introduire
                $("body").append("<input value='"+tb+"'>");
            });
        </script>

but i get just L into the input, what is the exact problem, i think the cause is that char ' , please help me fix it, and thnx !

1
  • i think the 'quotes' is the problem there. The "tb" value is applied before the input is append to body. Commented Apr 13, 2015 at 12:40

1 Answer 1

2

This is due to the type of quotes you're using to delimit the string in JS and the attributes in HTML - you need to swap them over as the value itself contains '. Try this:

$("body").append('<input value="' + tb + '">');

Working fiddle

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

2 Comments

And when the text has a " in it? Same issue.
@epascarello then you would need to escape the value, or use val() whcih does the escaping for you. For this issue, it's better to KISS.

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.