Unable to get javascript to set text

I see examples for all of this, but for some reason mine is not working. I have a text box that is added dynamically if a specific value is selected in the selected list.

The part where the field is displayed works, but I'm also trying to add text to the field that I cannot get. I am also trying to use JS to select text after typing it - but have not received it yet!

Is there anything wrong with this?

function showBox() {
  if (document.getElementById("ctl00_Content_WhereFound").value == "Other" || document.getElementById("ctl00_Content_WhereFound").value == "Friend/Employee Referral") 
  {
    document.getElementById('ctl00_Content_WhereDetails').style.display = "inline";
    if (document.getElementById("ctl00_Content_WhereFound").value == "Other") {
      document.getElementById('ctl00_Content_WhereDetails').innerHTML += 'Enter Other';
    } else {
      document.getElementById('ctl00_Content_WhereDetails').innerText += "Enter Referral";
    }
  }
}
+3
source share
4 answers

try it.

function showBox()
{
    $Found = document.getElementById("ctl00_Content_WhereFound");
    $Where = document.getElementById('ctl00_Content_WhereDetails');

    if($Found.value == "Other" || $Found.value == "Friend/Employee Referral")
    {
        $Where.style.display = "inline";
        if($Where.value == "Other")
        {
            $Where.value = 'Enter Other';
        }else
        {
             $Where.value = "Enter Referral";
        }
    }
}

You can always assign elements to variables to reduce code.

+2
source

, , , 'innerHTML' if 'innerText' else. ? ...

, , , document.createElement() etc / .

... , DOM, innerHTML , . JQuery .

+4

, , Asp.Net. , . , UniqueID ctl00_Content_WhereFound, ClientID ctl00$Content$WhereFound.

+1

innerTextnot supported, at least Firefox. Is there a reason you cannot use innerHTMLin both cases?

In addition, you might want to keep links to elements to make your code cleaner and faster:

function showBox() {
  var eFound = document.getElementById("ctl00_Content_WhereFound");
  if (eFound.value == "Other" || eFound.value == "Friend/Employee Referral") 
  {
    var eDetails = document.getElementById('ctl00_Content_WhereDetails');
    eDetails.style.display = "inline";
    if (eFound.value == "Other") {
      eDetails.innerHTML += 'Enter Other';
    } else {
      eDetails.innerHTML += "Enter Referral";
    }
  }
}
+1
source

Source: https://habr.com/ru/post/1764163/


All Articles