Claudia Claudia - 3 months ago 11
HTML Question

jQuery how to pass #output to variable to use in input field

First, I'm sorry about posting a question. I don't even know how to make a search for this, since my knowledge about Javascript nor jQuery is 0%. I tried searching the automatic questions that show up when you type your question, but I couldn't find the answer yet.

I have this piece of code to drag an image around inside a div:

$(function() {
$('.headerCoupleBackground').imagedrag({
input: "#output",
position: "middle",
attribute: "html"
});
}


So, I can show this on my HTML page by posting:

<span id="output"></span>


But, what I like to do, is to show the created output inside a hidden input field value. I think I need to change the 'input: #output'-part, but I really have no idea how to change that or what I need to do to fix my problem. Instead of saving it inside #output, I thought I needed to store it inside a variable, but I didn't got that to work either.

Thanks in advance.

Answer

Change the HTML to an input

<input id="output" type='hidden'>

And the attribute to value

$('.headerCoupleBackground').imagedrag({
    input: "#output",
    position: "middle",
    attribute: "value"
});

According to the docs, the default value for attribute is value, so you can omit it entirely:

$('.headerCoupleBackground').imagedrag({
    input: "#output",
    position: "middle"
})