0
$( window ).on( "load", function(){
        var token_val= document.getElementsByName("token")[0].value;

        var real_data = "<input type='hidden' name='token' value='NEED VLAUE OF token_val HERE'>";

        $('form').append(real_data);
});

This my code. I need to set value on input tag as variable token_val 's value.

1
  • Just concatenate the value in string like this -- ... value=' "+token_val+" '... Commented May 25, 2017 at 7:58

4 Answers 4

2

change your code with the below one

  $( window ).on( "load", function(){
            var token_val= document.getElementsByName("token")[0].value;

            var real_data = "<input type='hidden' name='token' value='"+token_val+"'>";

            $('form').append(real_data);
    });
Sign up to request clarification or add additional context in comments.

Comments

2

Use jQuery's object-based approach to creating elements.

var real_data = $('<input>', {
    type: 'hidden',
    name: 'token',
    value: token_val
});
$('form').append(real_data);

Comments

1

Others have beaten me to the answer, but if you fancy being progressive and using ES2015, you could use template strings:

let real_data = `<input type="hidden" name="token" value="${token_val}">`

1 Comment

@RoryMcCrossan whoops, typo :-|)
0
$( window ).on( "load", function(){
    var token_val= document.getElementsByName("token")[0].value;

    var real_data = "<input type='hidden' name='token' value='" + token_val + "'>";

    $('form').append(real_data);
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.