Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

value from span to input

I have value that is displayed in span tag. If i want to post the value i have to assign that value to an input. So this is the code i have written to assign value to input and trying to post that value. But when i alert the assigned value its showing as

[object Object] 

Pls check the code and correct me.

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

and i tried this also

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

Both the above code shows

[object Object] 

Because of this i am not able to post values.

like image 307
Varuni N R Avatar asked Jan 07 '16 06:01

Varuni N R


People also ask

Can you put a span in an input?

Wrap both your input and your span inside a container, position this container as relative, and the span as absolute. You can now do whatever you like with the span.

How do I change input to span on click?

First, you need to change your click handler to use live() as well. You should take note, though, that live() has been deprecated for quite a while now. You should be using on() in both cases instead. Secondly, when you replace the input with the span, you don't give the element an id.

Can span tag have value attribute?

a span doesn't have a value attribute.


2 Answers

That is because the value setter function on an input return the jQuery object of that input element only, it allows you to chain the event.

Try this

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

Read more about it here http://api.jquery.com/val/#val-value

like image 114
void Avatar answered Sep 30 '22 00:09

void


"lower" is an object. if you want to see the text inside, you need to call lower.val(). e.g.

var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower.val());
like image 34
liusy182 Avatar answered Sep 30 '22 00:09

liusy182