8 Replies - 379 Views - Last Post: 19 June 2019 - 10:03 PM

#1 Trigonatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-September 16

How to assign variable to innerHTML element

Posted 19 June 2019 - 09:26 AM

I have the following line and would like to assign it to a variable:
document.getElementById('field').innerHTML = document.getElementById("spice").value;


My objective is to capture the data in the .value element

is this possible? If so, how?

Thank you!
Is This A Good Question/Topic? 0
  • +

Replies To: How to assign variable to innerHTML element

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15109
  • View blog
  • Posts: 60,436
  • Joined: 12-June 08

Re: How to assign variable to innerHTML element

Posted 19 June 2019 - 09:33 AM

Why wouldn't you use a javascript variable?
Was This Post Helpful? 1
  • +
  • -

#3 Trigonatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-September 16

Re: How to assign variable to innerHTML element

Posted 19 June 2019 - 10:14 AM

Thank you for your reply.
I would like to use a javascript variable. Unfortunately, this doesn't seem to work:
var v1 = document.getElementById('field').innerHTML = document.getElementById("spice").value;


I don't know how to extract (or correctly target) data stored in the .value element and assign it to a variable. How is this done?
Was This Post Helpful? 0
  • +
  • -

#4 DarenR   User is offline

  • D.I.C Lover

Reputation: 616
  • View blog
  • Posts: 4,104
  • Joined: 12-January 10

Re: How to assign variable to innerHTML element

Posted 19 June 2019 - 11:05 AM

What are you trying to do exactly?


it seems a little weird to grab a value and set field the way you are----
Was This Post Helpful? 1
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15109
  • View blog
  • Posts: 60,436
  • Joined: 12-June 08

Re: How to assign variable to innerHTML element

Posted 19 June 2019 - 11:19 AM

Typically you would have one assignment.

var a = "foo";


Not multiple assignments.
var a = second_variable = "foo";

Was This Post Helpful? 1
  • +
  • -

#6 Trigonatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-September 16

Re: How to assign variable to innerHTML element

Posted 19 June 2019 - 11:23 AM

Thanks for your reply.
I simply want to assign the variable v1 = .value

After some trial and error, it looks as if this accomplishes the task:
var v1 = document.getElementById("spice").value


I wrongly assumed that "InnerHTML" was required to target .value
Was This Post Helpful? 0
  • +
  • -

#7 DarenR   User is offline

  • D.I.C Lover

Reputation: 616
  • View blog
  • Posts: 4,104
  • Joined: 12-January 10

Re: How to assign variable to innerHTML element

Posted 19 June 2019 - 12:39 PM

usually innerhtml is for output from a variable


like

something.innerHtml = someVariable.value

Was This Post Helpful? 1
  • +
  • -

#8 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2334
  • View blog
  • Posts: 7,111
  • Joined: 15-January 14

Re: How to assign variable to innerHTML element

Posted 19 June 2019 - 03:27 PM

That code should still work in theory, but maybe the DOM gets updated asynchronously and the element's innerHTML value hasn't actually been updated by the time it's read.
Was This Post Helpful? 0
  • +
  • -

#9 sagar.chavada   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-April 19

Re: How to assign variable to innerHTML element

Posted 19 June 2019 - 10:03 PM

Hey this not valid way to assign Inner HTML my point of view you have to use Javascript variable to assign Inner HTML value Like this

var Myfield = document.getElementById('field').innerHTML;
var Myspice = document.getElementById("spice").innerHTML;

you have to try this for your issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1