0

I have a two div I want to send selected text to another div using onSelect event. Right now entire para sending to another div but I want to send just selected text. How can I do this?

Demo: https://codesandbox.io/s/selected-text-send-to-another-div-using-onselect-0ccnrn?file=/src/App.js

My code:

import React from "react";
import { Box, Grid, TextField, Typography } from "@material-ui/core";
import { useState } from "react";

const SendSelectedText = () => {
    const [label1, setlabel1]=useState('');
    const [para, setPara]=useState('This is Old Para');
   const handleClick = () => {
    setlabel1(para);
  };
  return (
    <>
      <Box className="sendSelectedTextPage">
        <Grid container spacing={3}>
          <Grid item xl={6} lg={6} md={6}>
            <textarea onSelect={handleClick}>{para}</textarea>
          </Grid>
          <Grid item xl={6} lg={6} md={6}>
            <TextField
              variant="outlined"
              size="small"
              label="Label One"
              value={label1}
              multiline
              rows={3}
              className="sendSelectedTextPageInput"
            />
          </Grid>
        </Grid>
      </Box>
    </>
  );
};

export default SendSelectedText;

2 Answers 2

2

All you need is use window.getSelection(). Here is solution

import React from "react";
import { Box, Grid, TextField, Typography } from "@material-ui/core";
import { useState } from "react";

const SendSelectedText = () => {
  const [label1, setlabel1] = useState("");
  const [para, setPara] = useState("This is Old Para");
  const handleMouseUp = () => {
    setlabel1(window.getSelection().toString()); // setting up label1 value 
  };
  return (
    <>
      <Box className="sendSelectedTextPage">
        <Grid container spacing={3}>
          <Grid item xl={6} lg={6} md={6}>
            // changed event to onMouseUp 
            <textarea onMouseUp={handleMouseUp}>{para}</textarea>
          </Grid>
          <Grid item xl={6} lg={6} md={6}>
            <TextField
              variant="outlined"
              size="small"
              label="Label One"
              value={label1}
              multiline
              rows={3}
              className="sendSelectedTextPageInput"
            />
          </Grid>
        </Grid>
      </Box>
    </>
  );
};

export default SendSelectedText;

Sandboxlink

Sign up to request clarification or add additional context in comments.

Comments

2

You have to use the selection

const handleClick = (e) => {
  setlabel1(
    e.target.value.substring(e.target.selectionStart, e.target.selectionEnd)
  );
};

or

const handleClick = (e) => {
  setlabel1(
    para.substring(e.target.selectionStart, e.target.selectionEnd)
  );
};

Based on this

sandbox

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.