1

In my code there is a text input that lets the user type in their own text, this text is then turned into a variable;

var title = document.getElementById("chartTitle");
var xaxis = document.getElementsByName('xaxis')[0];
var yaxis = document.getElementsByName('yaxis')[0];

Now that the variables contain the text, I need to change two parts within this code to make it so the predefined "text" is replaced with the text within the variable. The two parts are "title text" must changed to be the var title and the "yaxis text" must be changed to the var yaxis.

 var chart = new CanvasJS.Chart("chartContainer", {
    title:{
      text: "title text"
    },
    axisY: {
      title: "yaxis text"
    },
    data: [{
         type: "column",
         dataPoints: []
    }]
});
chart.render();
}
3
  • add an event listener for those elements such as "change" or "keyup" events, and inside the function set the title.text etc. to the .textContent of those elements (you can use this inside the function). Commented Mar 26, 2018 at 3:43
  • Can you not simply set the correct text in the first place, ie title: { text: title }, axisY: { title: yaxis }? Commented Mar 26, 2018 at 3:44
  • can you add your html ? Commented Mar 26, 2018 at 3:44

3 Answers 3

1

Consider adding id to your inputs to access them faster using document.getElementById:

<input type="text" name="yaxis" id="yaxis">

Note that the functions document.getElementById and document.getElementsByName return DOM elements rather than their text. To access the content of text input element use its proerty value.

var chart = new CanvasJS.Chart("chartContainer", {
    title:{
      text: title.value
    },
    axisY: {
      title: yaxis.value
    },
    data: [{
         type: "column",
         dataPoints: []
    }]
});
chart.render();
}
Sign up to request clarification or add additional context in comments.

Comments

0

The variables do not contain the text; they contain only the elements.

If you're going to select a single element, it's better to use querySelector instead of one of the HTMLCollection methods:

const xAxisElement = document.querySelector('[name=xaxis]');
const yAxisElement = document.querySelector('[name=yaxis]');

Then to get their values, you need to use .value:

const xAxisValue = xAxisElement.value;
const yAxisValue = yAxisElement.value;

Then you can proceed to do things with xAxisValue and yAxisValue.

Comments

0

if i'm understanding correctly that you need to use the variables you defined earlier for the elements, and extract the value, just put them on your code like this:

var title = document.getElementById("chartTitle").value;
var xaxis = document.getElementsByName('xaxis')[0].value;
var yaxis = document.getElementsByName('yaxis')[0].value;

 var chart = new CanvasJS.Chart("chartContainer", {
    title:{
      text: title
    },
    axisY: {
      title: yaxis
    },
    data: [{
         type: "column",
         dataPoints: []
    }]
});
chart.render();
}

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.