简体   繁体   中英

Get content of <div> knowing its id

I am trying to get the content of a element of which I know partially the id.
The foo variable is correctly generated, but I cannot get it to return a chart object from the div element.
This is the jQuery code I am using:

$(".addViewport").click(function () {                   

  var foo = "#" + $(this).parent().children('div[id$="_chart"]').attr('id');

  var chart = foo.CanvasJSChart();

  chart.options.axisX.viewportMaximum += 86400000;
  chart.render();

});

And this is the html:

<div id="temperature">
    <div id="temperature_chart" style="height: 300px; width: 100%;"></div>
    <button class="addViewport">Viewport +</button>
</div>

The chart library I am using is called CanvasJS and it has a method called .CanvasJSChart(); to get the chart from specific div element. 截图

This line returns a string ( "#temperature_chart" ):

var foo = "#" + $(this).parent().children('div[id$="_chart"]').attr('id');

and that's why this line cannot work:

var chart = foo.CanvasJSChart();

foo needs to become a jQuery object in order for all that to work. For example:

var foo = $(this).parent().children('div[id$="_chart"]');

This worked for me in JSFiddle:

$(".addViewport").parent().children().get(0)

https://jsfiddle.net/fyvh0o56/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM