0

I need your help. I want to call a function and assign the result to inner html of an element but it doesn't work. Below is the code:

function abc() {
  var a = document.getElementById("take").value;

  function Ram() {
    document.write("your name is : Ram <br>");
    document.write("your vill/post : Sunsyari <br>");
    document.write("your block is : Betalghat <br>");
    document.write("your Total land is: 5 bega <br>");
  }

  if (a == "Ram" || a == "ram") {
    document.getElementById("show").innerHTML = Ram();
  } else {
    document.getElementById("show").innerHTML = "wrong credential";
  }
}

Please help what to do?

nikoss
  • 2,454
  • 2
  • 19
  • 34
  • 4
    You're supposed to assign a String to `.innerHTML`. But your `Ram` function returns `undefined`, so that's not going to work. It needs to `return "your name is...";` – blex Apr 26 '20 at 18:40
  • 2
    Also, [avoid `document.write`](https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice)! – Bergi Apr 26 '20 at 18:44
  • Try to avoid using document.write. There is almost never a good reason to ever use it in a web page. You are already assigning the value to innerHTML, so just return a string from Ram(). – Bryan Williams Apr 26 '20 at 18:57

1 Answers1

1

InnerHTML property accepts a string but in your case you are not actually returning anything from the Ram function. Try the following snippet

function abc() {
  var a = document.getElementById("take").value;

  function Ram() {
    return `your name is : Ram <br>
    your vill/post : Sunsyari <br>
    your block is : Betalghat <br>
    your Total land is: 5 bega <br>`
  }
  if (a.toLowerCase()=="ram") {
    document.getElementById("show").innerHTML = Ram();
  } else {
    document.getElementById("show").innerHTML = "wrong credential";
  }
}
abc()
#take{
background:yellow;
}
<input value="ram" id="take">
<div id="show"><div>
nikoss
  • 2,454
  • 2
  • 19
  • 34