74

I have a span with class="span" and a hidden field class="dropdown".

The span text changes, and I need to grab the text and set it as the value of the hidden field's value.

I will then use php (I already have) and use the name of the hidden field to email me the text.

How do I do it?

Nolwennig
  • 1,613
  • 24
  • 29
Hunter Mitchell
  • 801
  • 1
  • 7
  • 8

5 Answers5

95
<script type="text/javascript">
document.getElementById('button1').onChange = function () {
    document.getElementById('hidden_field_id').value = document.getElementById('span_id').innerHTML;
}
</script>
John Conde
  • 217,595
  • 99
  • 455
  • 496
60

var span_Text = document.getElementById("span_Id").innerText;

console.log(span_Text)
<span id="span_Id">I am the Text </span>
GuCier
  • 6,919
  • 1
  • 29
  • 36
maxspan
  • 13,326
  • 15
  • 75
  • 104
  • @MichaelStalcup The page you linked to says `innerText` has been adopted by all major browser vendors and is part of the HTML standard. Support is excellent: `IE 6 +` – Kilmazing Jan 04 '17 at 17:42
  • @Kilmazing thanks for pointing that out, I'll delete my old comment since I can't edit it. Mozilla must have updated their page since when I was looking at it! Or else I misread something. There's some interesting additional reading about innerText on [Mozilla's dev site](https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText) now if you look at Specification > Comment – Michael Stalcup Jan 05 '17 at 16:06
  • This is better if your `span` element has child elements and you only need the text – Novastorm May 04 '17 at 15:17
  • based on your posting, I used the command `document.getElementById("span_Id").innerText;` by itself in KeyboardMaestro to pull span fields in for automation - works great. Thanks @maxspan – AFK Dec 20 '20 at 23:50
15

The accepted answer is close... but no cigar!

Use textContent instead of innerHTML if you strictly want a string to be returned to you.

innerHTML can have the side effect of giving you a node element if there's other dom elements in there. textContent will guard against this possibility.

wle8300.com
  • 2,588
  • 1
  • 23
  • 29
5

You need to change your code as below:

<html>
<body>

<span id="span_Id">Click the button to display the content.</span>

<button onclick="displayDate()">Click Me</button>

<script>
function displayDate() {
   var span_Text = document.getElementById("span_Id").innerText;
   alert (span_Text);
}
</script>
</body>
</html>
demongolem
  • 9,474
  • 36
  • 90
  • 105
Vishal Thakur
  • 1,564
  • 16
  • 25
2

Judging by your other post: How to Get the inner text of a span in PHP. You're quite new to web programming, and need to learn about the differences between code on the client (JavaScript) and code on the server (PHP).

As for the correct approach to grabbing the span text from the client I recommend Johns answer.

These are a good place to get started.

JavaScript: https://stackoverflow.com/questions/11246/best-resources-to-learn-javascript

PHP: https://stackoverflow.com/questions/772349/what-is-a-good-online-tutorial-for-php

Also I recommend using jQuery (Once you've got some JavaScript practice) it will eliminate most of the cross-browser compatability issues that you're going to have. But don't use it as a crutch to learn on, it's good to understand JavaScript too. http://jquery.com/

Community
  • 1
  • 1
Jack
  • 15,614
  • 19
  • 67
  • 92