0

I have used the below JavaScript code to get the sum value of list column and the display it in a div.

<script type="text/javascript">
 _spBodyOnLoadFunctionNames.push("Effort");   
function Effort(){
var effortSpent = document.getElementById("aggrWPQ3").lastElementChild.innerText;
var IntEffortSpent = effortSpent.substring(4);
var results = parseInt(IntEffortSpent) + 0;

document.getElementById("DIV").innerHTML=results ;

    };
</script>

This code works when there is some item in the list and it is able to get the sum value and display it in the div but when there is no item in the list,I want it to display 0 in the same div.

what is needed to be done?

3
  • what does IntEffortSpent return when list is empty? get its value by console.log(IntEffortSpent); Sep 24, 2015 at 5:25
  • effortSpent will be like sum=14(when item in list) and when no item in list it showed error when debugged in document.getElementById("aggrWPQ3").lastElementChild.innerText; line of code with red line under lastElementChild.innerText;
    – KumarV
    Sep 24, 2015 at 5:41
  • see my answer now Sep 24, 2015 at 6:04

1 Answer 1

2

Based you comment the solution should be

function Effort() {
    var results = 0;
    if (document.getElementById("aggrWPQ3")) {
        var effortSpent = document.getElementById("aggrWPQ3").lastElementChild.innerText;
        var IntEffortSpent = effortSpent.substring(4);
        results = parseInt(IntEffortSpent) + 0;
    }
    document.getElementById("DIV").innerHTML = results
};

I am assuming that document.getElementById("aggrWPQ3") is undefined. document.getElementById("aggrWPQ3").lastElementChild could be undefined also. In that case, solution should be

function Effort() {
    var results = 0;
    if (document.getElementById("aggrWPQ3").lastElementChild) {
        var effortSpent = document.getElementById("aggrWPQ3").lastElementChild.innerText;
        var IntEffortSpent = effortSpent.substring(4);
        results = parseInt(IntEffortSpent) + 0;
    }
    document.getElementById("DIV").innerHTML = results
};

Let me know which one has worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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