0

How to get data from JavaScript getelementbyid insert into Html input text? Here is my sample code:

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var currentUser;
function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}

function onQuerySucceeded() {
    document.getElementById('userLoginName').innerHTML = ''; 
    document.getElementById('userId').innerHTML = '';
    document.getElementById('userTitle').innerHTML = currentUser.get_title();
    document.getElementById('userEmail').innerHTML = '';
}

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>
<div>Current Logged User:
<!--     <span Name="userLoginName"></span>
    <span Name="userId"></span>
    <span Name="userTitle"></span>
    <span Name="userEmail"></span> -->

    <input type="text" name="userLoginName" id="userLoginName">
    <input type="text" name="userId" id="userId">
    <input type="text" name="userTitle" id="userTitle">
    <input type="text" name="userEmail" id="userEmail">
</div>
  • Hi, i think you should use document.getElementById('#userTitle').innerHTML if userTitle is a text box with ID "userTitle" – vinit kumar Aug 16 '18 at 7:13
  • In between this question should be asked in Javascript stack exchange – vinit kumar Aug 16 '18 at 7:15
2

Since you're using textboxes, just change the display means from innerHTML to value in your success block -

function onQuerySucceeded() { 
    document.getElementById('userLoginName').value= currentUser.get_loginName(); 
    document.getElementById('userId').value= currentUser.get_title();; 
    document.getElementById('userTitle').value= currentUser.get_title(); 
    document.getElementById('userEmail').value= currentUser.get_email();
}
  • @junreyd pls mark the answer completed if it resolves ur issue :) – Satyajit Paul Aug 16 '18 at 8:57
1

You update/add value attribute of element like below

document.getElementById('userTitle').setAttribute("value", currentUser.get_title());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.