How to assign a variable value to an HTML input created in Javascript / How to style HTML elements in Javascript


#1

Here's the code I am currently using.

function firstChildAge() { 
                        var header = document.createElement('H1'); 
                        var body = document.getElementsByTagName('BODY'); 
                        var textnode = document.createTextNode("WHAT IS THE AGE OF THE FIRST CHILD?"); 
                        var inputChildOne = document.createElement("Input");
                        header.appendChild(textnode); 
                        document.body.appendChild(header); 
                        document.body.appendChild(inputChildOne);
            }

a) How would I assign a variable value to the response created by the user to the Input?

b) How can I style elements inside Javascript? For example, how can I make the text "WHAT IS THE AGE OF THE FIRST CHILD?" red, or change the font size?

Thank you!!!


#2

inputElement.value
will return the content of the html input.

as for styling, you can either do something like
theElement.style = "color:red; font-size: 20px";
or if you only want to apply one thing
theElement.style.color = "red";
(Although I'd recommend the first method)


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.