1

I am trying to capitalize the first letter of each word in the other function.

This is the function to turn each words first letter to capital that I am using.

function capital_letter(str) 
{
    str = str.split(" ");

    for (var i = 0, x = str.length; i < x; i++) {
        str[i] = str[i][0].toUpperCase() + str[i].substr(1);
    }

    return str.join(" ");
}

Here’s the second function that I’m trying to use the first one in, but it is not working.

$(document).ready(function() {
  $('#list').dataTable({
    "ajax":{
          url :"list.php",
          type: "GET",
          error: function(){
            $("#post_list_processing").css("display","none");
          }
        },
        "columns": [
              { "data": function capital_letter(item) {
                  return item.title;
                }
              },
              { "data": "description" },
              { "data": "time" }
          ]
  });
});
3
  • You're not calling the function, you're redefining it. Should be "data": capital_letter(item.title), (or item, whichever contains the text you want to modify) Commented Sep 24, 2018 at 11:20
  • Try to define the function outside and call it inside from the another function Commented Sep 24, 2018 at 11:22
  • Can you put your code on codepen? Commented Sep 24, 2018 at 11:33

3 Answers 3

1

You should only call the function like the following:

$(document).ready(function() {
  $('#list').dataTable({
    ajax: {
      url: 'list.php',
      type: 'GET',
      error: function() {
        $('#post_list_processing').css('display', 'none');
      },
    },
    columns: [{
        data: capital_letter(item.title);

      },
      { data: 'description' },
      { data: 'time' },
    ],
  });
});
Sign up to request clarification or add additional context in comments.

Comments

1

You're redefining the function instead of actually using it... Call it like this;

...
"columns": [
    { "data": capital_letter(item.title) }
...

Comments

0

You are not calling the function correctly. Change the following in your code.

"columns": [
    { "data": capital_letter(item.title) }

Created a working snippet.

function capital_letter(str) {
  str = str.split(" ");

  for (var i = 0, x = str.length; i < x; i++) {
    str[i] = str[i][0].toUpperCase() + str[i].substr(1);
  }

  return str.join(" ");
}

const columns = [{
    "data": capital_letter("abc")
  },
  {
    "data": "description"
  },
  {
    "data": "time"
  }
]

console.log(columns)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.