Value from range to input

I have a value that appears in the span tag. If I want to publish a value, I must assign this value to the input. So this is the code that I wrote to assign a value to the input and try to publish that value. But when I warn the assigned value, showing it as

[object Object] 

Pls check the code and correct me.

var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower);

and I tried it also

var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower);

Both of the above codes show

[object Object] 

Because of this, I cannot send values.

+4
source share
6 answers

This is because the input setterter function returns only the jQuery object of this input element, it allows you to bind an event.

try it

var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower.val());

http://api.jquery.com/val/#val-value

+5

"" - . , lower.val(). .

var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower.val());
+2

jQuery .

, , ( .val() getter, .

, .

var lower=$("#inputElement").val(value);
alert(lower);,

#lower.

.

:

var lower=$("#inputElement").val(value).css('color', 'red');

.

, :

var value = $("#spanElement").text();
$("#inputElement").val(value);
var lower=$("#inputElement").val();
alert(lower);
+1

.

var span_text = $("#spanElement").text(); //get span text
$("#inputElement").val(span_text); //set span text to input value
var input_value = $("#inputElement").val(); //get input value
alert(input_value); //alert input value

,

+1

This is because it will return a jQuery object. If you want to see the same text that is in the input, you can either use .val()it as before, or use it as

var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert($("#spanElement").prop('innerHTML'));

WORKING DEMO

+1
source

You get an “Object Object” because lower is an object if you want to get the text needed to complete the steps below.

var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower.val());

Or

var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower.val());

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1623204/


All Articles