user1823055
user1823055

Reputation: 91

Contents From Two Divs to One Input Field Value

I'm trying to insert the contents of the two divs into a single input field in a form. This is what I have so far but at the moment the only field that copies over when the onclick occurs is the edit1 div contents.

any help would be greatly appreciated.

The JAVASCRIPT:

<script language="javascript" type="text/javascript">
function copyText2() {
var output = document.getElementById("edit1","edit2").innerHTML;
document.getElementById("user-submitted-tags").value = output;
}
</script>

The HTML:

<div id="edit1">foo</div>
<div id="edit2">bar</div>

<form>
<input class="usp_input" type="text" name="user-submitted-tags" id="user-submitted-tags" value="">

<input onClick="copyText2()" class="usp_input" type="submit" name="user-submitted-post" id="user-submitted-post" value="Submit Post">
</form>

Upvotes: 0

Views: 595

Answers (4)

SteveP
SteveP

Reputation: 19093

Get the two contents separately, and then add them.

var output1 = document.getElementById("edit1").innerHTML;
var output2 = document.getElementById("edit2").innerHTML;
document.getElementById("user-submitted-tags").value = output1 + "&nbsp;" + output2;

Upvotes: 1

Cianan Sims
Cianan Sims

Reputation: 3429

document.getElementById("") is only using the first id string you give it. It doesn't accept multiple ids. From the Mozilla Dev docs:

id is a case-sensitive string representing the unique ID of the 
element being sought.

Just have output grab the content from the first id, then from the second and append them together.

Upvotes: 0

the blizz
the blizz

Reputation: 330

Change

var output = document.getElementById("edit1","edit2").innerHTML;

into

var output = document.getElementById("edit1").innerHTML + document.getElementById("edit2").innerHTML;

That should work.

Upvotes: 1

codingbiz
codingbiz

Reputation: 26386

You should copy them one at a time and then concatenate

function copyText2() {
    var output = "";

    output += document.getElementById("edit1").innerHTML;

    output += document.getElementById("edit2").innerHTML;

    document.getElementById("user-submitted-tags").value = output;
}

Upvotes: 0

Related Questions