I am trying to pass a table variable as a mailto subject.
I am using this script to do for a div ID, but unsure how to to do this for a class - getElementsByClassName
doesn't work. How Do I do this?
Button:
<button name="mail_button" onclick="mail_content()">Mail</button>
Script:
function mail_content() {
var tableContent = document.getElementById("mailcontent").innerHTML;
var mBody = "" + tableContent + "";
document.location.href = "mailto:test@domain.com?subject=Titel&body=" + mBody;
}
Element I wish to fect the varable from:
<td class="mailcontent">N7P47AA</td>
You have to use the index.Class is not unique like the id.
document.getElementsByClassName("mailcontent")[0].innerHTML
1) Select it via an ID that is unique.
2) Select it via a Class and index combination.
Here is the Docs for it: https://developer.mozilla.org/en/docs/Web/API/Document/getElementsByClassName
function mail_content() { var tableContent = document.getElementsByClassName("mailcontent")[0].innerHTML; var mBody = "" + tableContent + ""; document.location.href = "mailto:test@domain.com?subject=Titel&body=" + mBody; } mail_content();
<table> <td class="mailcontent">N7P47AA</td> </table>
You are doing document.getElementById("mailcontent"), but the element you are looking for has no ID attribute. Currently your element has a class attribute of mailcontent, so you have 2 options:
Change document.getElementById to:
document.getElementsByClassName("mailcontent")[0].innerHTML
Remember, document.getElementsByClass name returns an array, rather than a single item, hence the addition of the [0] so you can get the first item in the array
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.