1

I have a quick question regarding being able to store parts of an element as a string which I can later recall.

Example: I have an element that looks like this

<input type= "hidden" name="Posted" id="Posted" value="100|307|151|16">

I can retrieve the element with document.getElementById('#Posted') and now I want to be able to take the contents in the [value] tag and store them as a string in a new variable, so I can get something like this:

var inputValue = "100|307|151|16"

Which I can later recall in my code. Maybe I'm blind but after a bit of searching I've still come up with nothing. Thanks in advance for the help!

agentzulu
  • 11
  • 2

3 Answers3

1
 //If all you care about is the value, you can just use .value to get the information you want
const inputValue = document.getElementById('#Posted').value;
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
1

There's two different approaches you can take here.

You can either get the value of the hidden input, or you can get the value of the "value" attribute on the hidden input.

Here's how to do both:

var element = document.getElementById('Posted');

var value = element.value;
var attribute = element.getAttribute("value");

console.log(`value: ${value}`);
console.log(`attribute: ${attribute}`);

And here's a JSFiddle of that in action.

  • 1
    *Isn't your answer basically the same as the two others?* Pretty much but with a handful of important differences. I supplied explanations, sources for documentation, a working example, and my code works. –  Nov 26 '18 at 17:22
0

Have you tried:

var inputValue = document.getElementById('#Posted').getAttribute("value");
Steven
  • 442
  • 3
  • 9