admin管理员组

文章数量:1290313

I have two html elements:

<input id="searchInput">
<div id="searchText"></div>

I'm using d3 to add a listener in a script.

d3.select("#searchInput").on("keyup",getSearchText);

I need a function that prints a value of input to a div

function getSearchText(value) {
d3.select("#searchResults").html(value.toString());
}

But I don't know how to pass input value parameter to a listener function.

I have two html elements:

<input id="searchInput">
<div id="searchText"></div>

I'm using d3 to add a listener in a script.

d3.select("#searchInput").on("keyup",getSearchText);

I need a function that prints a value of input to a div

function getSearchText(value) {
d3.select("#searchResults").html(value.toString());
}

But I don't know how to pass input value parameter to a listener function.

Share Improve this question edited Jan 20, 2020 at 18:28 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 8, 2013 at 13:26 nextstopsunnextstopsun 4738 silver badges14 bronze badges 3
  • Not too familiar with d3, but shouldn't this.value give you the value? – tymeJV Commented Jul 8, 2013 at 13:30
  • Tried that. d3.select("#searchInput").value returns undefined. – nextstopsun Commented Jul 8, 2013 at 13:41
  • 1 No no, try using d3.select("#searchResults").html(this.value); – tymeJV Commented Jul 8, 2013 at 13:43
Add a ment  | 

3 Answers 3

Reset to default 4

I've found solution:

d3.select("#id").node().value

Try using an instance of this to get the value:

d3.select("#searchResults").html(this.value);

Use the third argument:

function getSearchText(d, i, o) {
  const myValue = o[0].value;
}

本文标签: javascriptGet value of input element in event listener with d3Stack Overflow