Note that there are some explanatory texts on larger screens.

plurals
  1. POError trying to use getElementsByName in order to choose multiple "placeholders" (JavaScript)
    primarykey
    data
    text
    <p>This script of mine has a function, which is not working the way, I want it to. Take a look at the script, the function is called display_Value():</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;JavaScript&lt;/title&gt; &lt;!--Scripts--&gt; &lt;script type="text/javascript"&gt; &lt;!-- function noValue(id) { var _value = document.getElementById(id).value; if (_value == "Skriv dit navn her") { document.getElementById(id).value=''; } } function changeValue(id) { var _value = document.getElementById(id).value; if (_value == "") { document.getElementById(id).value='Skriv dit navn her'; } } function display_Value() { var val = document.getElementById("txt").value; if (val != "Skriv dit navn her" &amp;&amp; val != "") { document.getElementsByName("placeholder").innerHTML = val; document.getElementById("hidden").style.visibility="visible"; destroy("destroy") } } function destroy(id) { var d = document.getElementById(id); d.parentNode.removeChild(d); } //--&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="destroy"&gt; &lt;input type="text" id="txt" value="Skriv dit navn her" onFocus="noValue('txt')" onBlur="changeValue('txt')"&gt; &lt;input type="button" onClick="display_Value()" value="Click me" /&gt; &lt;/div&gt; &lt;div id="hidden" style="visibility: hidden"&gt; &lt;p&gt;Hello. Your entered value is: &lt;span name="placeholder"&gt;&lt;/span&gt; and it is repeated below&lt;/p&gt; &lt;span name="placeholder"&gt;&lt;/span&gt; &lt;span name="placeholder"&gt;&lt;/span&gt; &lt;/div&gt; </code></pre> <p> </p> <p>The functions noValue() and changeValue() makes the original value disappear on focus and come back if the field is empty on blue. That part works. When the button is clicked, the value in the text box is supposed to be displayed in the <code>&lt;span name="placeholder"&gt;</code>-tags. That's not happening. The hidden text which is hidden with <code>&lt;div style="visibility:hidden"&gt;</code> is shown, as I want it to, but the value is not displayed. The form gets destroyed, just as I want to. Where have I made a mistake? :)</p> <p>Ps.: If you are wondering what "Skriv dit navn her" means, it means "Write your name here" in danish.</p> <h2>EDIT</h2> <p>I isolated parts of the code once again. I found out, that if I replace <code>getElementsByName("placeholder").innerHTML;</code> with <code>getElementById("placeholder").innerHTML;</code> and assigns the first <code>&lt;span&gt;</code> with and ID="placeholder" the value is displayed in that particular <code>&lt;span&gt;</code>.This is, however, not exactly what I wanted. I wanted the value to be displayed in all 3 of the placeholders - <strong>how?</strong></p> <h2>ANOTHER EDIT</h2> <p>I know it is possible to make this script work by assigning ID's to all of the placeholder; I'm not interested in that. I would like the function to display the value in all of the placeholders in as little text as possible and without repeating. I think this is called DRY / DIE :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload