Happy New Year

Set element value in JavaScript

The visibility property allows to set an element value. I am sharing a simple code here that how easily you can get a element value & text using JavaScript.

Set element value by id

<div><input id="firstname" type="text" value="c-sharphub" /></div>

$(function() {
  document.getElementById("firstname").value="c-sharphub-javascript";
});
or
function SetFirstName(){
  document.getElementById("firstname").value="c-sharphub-javascript";
}

Set element value by class

<div><input class="nameclass" type="text" value="c-sharphub" /></div>

$(function() {
  document.getElementsByClassName("nameclass").value="c-sharphub-javascript";
});
or
function SetvalueByClass(){
  document.getElementsByClassName("nameclass").value="c-sharphub-javascript";
}

Set element text by id

<div><span id="spanText">c-sharphub</span> </div>

$(function() {
  document.getElementById("spanText").innerHTML="c-sharphub-javascript";
});
or
function SetspanText(){
  document.getElementById("spanText").innerHTML="c-sharphub-javascript";
}

Set element text by class

<div><span class="spanText">c-sharphub</span> </div>

$(function() {
  document.getElementsByClassName("spanText").innerHTML="c-sharphub-javascript";
});
or
function SetspanText(){
  document.getElementsByClassName("spanText").innerHTML="c-sharphub-javascript";
}