javascript - How to get value from contentEditable in reactjs - Stack Overflow

I'm fairly new to ReactJS. I am looking to get the value inside a <div> when contentEditable

I'm fairly new to ReactJS. I am looking to get the value inside a <div> when contentEditable is set to true.

const listResults = this.state.results['1'].map((result) =>
  <div key={result.toString()} contentEditable="true">
    {result}
  </div>
);

return (
  <h1> listResults </h1>
  <div> {listResults} </div>
)

I am currently outputting a list into pre-filled text-boxes which allows the user to edit them. I am looking to add in a button which once clicked captures the data inside all of the text-boxes. Can anyone point me in a direction on how to capture the changed data.

It may also be worth noting I am using ReactJS on the client side through a CDN.

I'm fairly new to ReactJS. I am looking to get the value inside a <div> when contentEditable is set to true.

const listResults = this.state.results['1'].map((result) =>
  <div key={result.toString()} contentEditable="true">
    {result}
  </div>
);

return (
  <h1> listResults </h1>
  <div> {listResults} </div>
)

I am currently outputting a list into pre-filled text-boxes which allows the user to edit them. I am looking to add in a button which once clicked captures the data inside all of the text-boxes. Can anyone point me in a direction on how to capture the changed data.

It may also be worth noting I am using ReactJS on the client side through a CDN.

Share Improve this question edited Aug 18, 2017 at 16:37 Gleb Kemarsky 10.4k7 gold badges47 silver badges71 bronze badges asked Aug 18, 2017 at 15:46 JayJay 751 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

To get value of editable div:

class App extends React.Component {
    constructor(){
        super();
      this.state = {
            arr: [1,2,3,4,5]
      }
      this.change = this.change.bind(this);
  }
  change(e, index){
    let tmpArr = this.state.arr;
    tmpArr[index] = e.target.textContent;
    this.setState({arr: tmpArr})
  }
  render(){
    console.log(this.state);
    return (
      <tr>
          {this.state.arr.map((el, index) => <td key={index} id="test" onBlur={(e) => this.change(e, index)} contentEditable="true">{el}</td>)}
      </tr>
    );
  }
}

https://jsfiddle/69z2wepo/84647/

One note, you can't return two elements on the same level:

return (
  <h1> listResults </h1>
  <div> {listResults} </div>
)

It should be wrapped like this:

return (
    <div>
      <h1> listResults </h1>
      <div> {listResults} </div>
    </div>
)

To get the value of an element whose contentEditable attribute is set to true in React, here is a way to do it:

import React, { useRef } from "react";

export default () => {
  const editable = useRef(null);
  return (
    <>
      <p contentEditable={true} ref={editable} />
      <button
        onClick={() => {
          console.log(editable.current.innerHTML);
        }}
      >
        Click
      </button>
    </>
  );
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745583682a4634403.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信