D3.JS: Variable with getElementById default value

Asked

Viewed 30 times

1

I am creating a visualization in D3.js, in which I am importing the date through a dynamic string that requires a variable, which I named honeystring.

Variable

var honeyyear = document.getElementById("vardatayear");

Import

d3.json( `data/HoneyProduction-${honeyyear}.json`, function( honey_data ){...

Taking into account that Id element vardatayear does not exist before the display exists, as I guarantee that the variable has a value, such as 2013, if the element does not exist or is null?

That is, what you should do to assign a variable value, by default?

  • You want your var honeyyear comes with value 2013 if the element does not exist or is null?

  • Yes @Otaviocapel

1 answer

2


You can do it like this:

var honeyyear = document.getElementById("vardatayear") || 2013;

case document.getElementById("vardatayear") is null or does not exist to var honeyyear comes with 2013 value.

Or with a if you can too:

var honeyyear = document.getElementById("vardatayear")

if(!honeyyear){
    honeyyear  = 2013
}
  • The GET request is being made to: HoneyProduction-[object%20HTMLHeadingElement].json instead of HoneyProduction-2013.json

  • Changing the import to d3.json( data/Honeyproduction-${honeyyear ? honeyyear : 2013}. json, function( honey_data ){... also results in a request made in the wrong place

  • 1

    try to use var honeyyear = document.getElementById("vardatayear").value

Browser other questions tagged

You are not signed in. Login or sign up in order to post.