0

How can i show the name "First Textbox Name", I have tried a multitude of things but nothing seems to work.

This is the text box among other textboxes.

<td>FirstTextbox Name: 
   <input id="box1" name="box1" class="nosonly" type="text" oninput="calculate()" /></td> 
</td>    

<script>
       var x =  document.getElementById("box1").WhatwouldGohere?

       alert(x);

</script>

4 Answers4

2

Textboxes don't have a closing tag and therefore cannot have any innerHTML.

But, you can access other aspects of a textbox. Also, don't use inline HTML event attributes (onclick, onmousover, etc.). Even though you see them used everywhere, it's only because a lot of new JavaScript folks pick up bad habits. There are many reasons not to use them.

// Get a reference to the textbox
var tb = document.getElementById("box1");

// Set up the event handler in JavaScript, not HTML
tb.addEventListener("input", calculate);

function calculate(){
  // You can access HTML attributes as object properties:
  console.clear();
  
  // To get the content of the parent element, use the parentElement property
  // Then, access the textContent of that element to only get text (and not
  // nested child elements). Finally, strip off any leading or trailing 
  // spaces from that value (if desired) with .trim()
  var parentText = this.parentElement.childNodes[0].textContent.trim();
  
  alert("The text that preceeds the textbox is: " + parentText);
  console.log("The name of the textbox is: " + box1.name);
  console.log("There are " + this.value.length + " characters in the box.");
  console.log("The value of the box is: " + this.value);
}
<td>FirstTextbox Name: 
   <input id="box1" name="box1" class="nosonly" type="text"> 
</td>

But, your question has asked about the textbox's "label" and it turns out that there is actually a <label> element that you can and should use because it creates a more accessible UI and makes this even easier:

// Get a reference to the textbox and the label
var tb = document.getElementById("box1");
var lbl = document.querySelector("label[for=box1]");

// Set up the event handler in JavaScript, not HTML
tb.addEventListener("input", calculate);

function calculate(){
  // You can access HTML attributes as object properties:
  console.clear();  
  alert("The text that preceeds the textbox is: " + lbl.textContent);
  console.log("The name of the textbox is: " + box1.name);
  console.log("There are " + this.value.length + " characters in the box.");
  console.log("The value of the box is: " + this.value);
}
<td>
   <label for="box1">FirstTextbox Name:</label> 
   <input id="box1" name="box1" class="nosonly" type="text"> 
</td>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • That shows the name element. I was looking to show the label of the textbox.Sorry for not formatting the html properly. – user2799983 Sep 12 '17 at 21:22
  • 1
    @user2799983 Your post does not show any label being used. – Scott Marcus Sep 12 '17 at 21:24
  • @user2799983 See my updated answer for what you are looking for. – Scott Marcus Sep 12 '17 at 21:35
  • I can add a label element but not all the textboxes need one or should have one. – user2799983 Sep 12 '17 at 21:43
  • @user2799983 Actually, best-practices dictate that any form field that has any kind of descriptive information about it should have a label. But, even if you don't use one, my first code snippet shows how to get the data you want. – Scott Marcus Sep 12 '17 at 21:45
  • `document.querySelector("label[for=box1]");` you don't have to search the whole document as the label is defined in the scope of the parent. The current version of the question doesn't include any label tag. – Reflective Sep 12 '17 at 22:35
  • @Reflective I know the question doesn't include a label. If you read my answer, you'll see that I created a second snippet that uses one and explained why one should be used. The particular selector is irrelevant to the question or answer. – Scott Marcus Sep 12 '17 at 22:59
  • You were so alphabetical in your comments so I just showed you how is to be alphabetical :) Searching the whole document instead a smaller scope is really a bad practice!!! I'm mentioning that because you are talking about good practices, so it is not irrelevant. – Reflective Sep 12 '17 at 23:07
  • @Reflective I've been called a lot of things, but I don't think alphabetical has ever been one of them! – Scott Marcus Sep 12 '17 at 23:47
  • Well, something new came in your life ;) – Reflective Sep 13 '17 at 00:51
  • Thank you for all the help guys, I eventually followed what you suggested and added a label to the field and this is how i ended by getting the label "$(this).prev("label").html()". The label lets the users know on which field they had an incorrect input. – user2799983 Sep 13 '17 at 15:31
1

Well, you do a basic mistake which many people does having no experience with the DOM model. Keep in mind that the code should be executed after the DOM is initialised, so if you want to show the name of the textarea do the following as it's visible that you are using jQuery:

<td>
  <label>Label for Box 1</label>
  <textarea id="box1"></textarea>
</td>

<script>
$(function() {
   alert("My box name is "+ $('#box1').prevAll('label').html());
});
</script>

if no label tag (which is not very clever BTW):

<td>FirstTextbox Name: 
   <input id="box1" name="box1" class="nosonly" type="text" oninput="calculate()" /></td> 
</td> 

<script>
$(function() {
   alert("My box name is "+ $('#box1').parent().text());
});
</script>

Your initial code was showing that you are using jQuery.

This is a simple representation of your html combined with the code above:

alert($('<div>').html('<td>FirstTextbox Name: <input id="box1" name="box1" class="nosonly" type="text" /></td>').find('#box1').parent().text());

alerts FirstTextbox Name:

Reflective
  • 3,854
  • 1
  • 13
  • 25
  • You are using the `label` element incorrectly. It should either have a `for` attribute or it should wrap the element it is a label for. Also, although the original question used JQuery, that has since been removed and the question is not tagged with JQuery. – Scott Marcus Sep 12 '17 at 21:42
  • This seems to add the function on the alert box instead of just the alert – user2799983 Sep 12 '17 at 21:55
  • for is not a mandatory attribute, of course it's proper to be used, but I'm just covering the most common case. The original question was changed many times, it wasn't clear what is actually asked, so I won't spend time completing all the posible variations. – Reflective Sep 12 '17 at 22:30
  • You're right that 'for' is not mandatory, but when it is not used, the label should wrap the element it is a label for. Your code is not showing either of the two proper uses of label and that is not the most common case for its use. The way you have it, you won't get any of the UX benefits for both sighted and visually impaired users and might as well use a 'span'. – Scott Marcus Sep 12 '17 at 23:03
  • I know what the for attribute is and I use it, but can you imagine that I can set a data attribute keeping the same id of the textarea?

    Label

    getting some google advantages? Well, I don't want to be involved in useless discussions, covering the good practices area.
    – Reflective Sep 12 '17 at 23:12
0

Try this

<script>
function calculate() {
   var x =  $('#box1').val();
   alert(x);
}
</script>
0
<script>
   function calculate() {
      var x =  $('#box1').attr('name')
      alert(x);
   }
</script>

That would alert the name of your textbox.

Or given your edit you could do:

<script>
   function calculate() {
     var x =  document.getElementById("box1").getAttribute('name');
     alert(x);
   }
</script>