-1

I am trying to access the the text field with the id="first" so that i can potentially read what ever information is in it or even write information to it through java script.

This is the last method that i tried, i know that would not have worked but my brain is tired, I've been at this for days. I'm new to java script, and i'm just toying around with this as an experiment but now its driving me crazy.

<script>
 { var targetDiv = document.getElementById("popover-content- 
  right").getElementsByClassName("well popover- 
  right").getElementById("edit- 
  user").getElementById("first")[0];
  targetDiv.textContent = "Hello world!";
}

</script>

I've tried several other methods but non have worked.

<div id="popover-content-right" class="well popover-right" data- 
dicomgrid-active-popover="create" style="top: 133px; width: 480px; 
display: 
block;"> 
<form id="edit-user" onsubmit="return saveForm(this)">
<div class="form-inline">
  <div class="form-group">
    <label data-i18n-token="global:first-name">First name</label><br>
    <input id="first" data-validate="required" class="form-control">
    <span class="help-inline small"></span>
  </div>
  </div>
  </form>
  </div>

I'm wanting that when i write something to the text field that it shows up there e.g "Hello world", but right now i get nothing, it just stays blank.

Any help is greatly appreciated. Thanks

  • You were very close. Do this: `documentgetElementById("first").textContent = "Hello world!";; ` – Randy Casburn Jun 13 '19 at 20:33
  • 1
    Possible duplicate of [Set the value of an input field](https://stackoverflow.com/questions/7609130/set-the-value-of-an-input-field) – Herohtar Jun 13 '19 at 20:33

1 Answers1

1

Just a simple document.getElementById should return you the desired element, and .value makes you access the element's value, just like a normal variable.

onload = function() {
  var textField = document.getElementById("first");
  var value = textField.value; //read
  textField.value = "something"; //write
}
<div id="popover-content-right" class="well popover-right" data- 
dicomgrid-active-popover="create" style="top: 133px; width: 480px; 
display: 
block;"> 
<form id="edit-user" onsubmit="return saveForm(this)">
<div class="form-inline">
  <div class="form-group">
    <label data-i18n-token="global:first-name">First name</label><br>
    <input id="first" data-validate="required" class="form-control">
    <span class="help-inline small"></span>
  </div>
  </div>
  </form>
  </div>
Anis R.
  • 6,656
  • 2
  • 15
  • 37