en:text

<!DOCTYPE html> <html> <head>

<meta charset="UTF-8">
<title>Text Editor with Image Replacement</title>

</head> <body>

<div contenteditable="true" id="editor">
  Enter your text here...
</div>
<br>
<label for="img-file">Choose an image to replace text:</label>
<input type="file" id="img-file">
<br>
<button onclick="replaceTextWithImage()">Replace Text with Image</button>
<script>
  function replaceTextWithImage() {
    const editor = document.getElementById("editor");
    const selectedText = window.getSelection().toString();
    const fileInput = document.getElementById("img-file");
    const file = fileInput.files[0];
    
    if (selectedText !== "" && file) {
      const reader = new FileReader();
      reader.onload = function(event) {
        const imgSrc = event.target.result;
        const imgElement = '<img src="' + imgSrc + '">';
        const replacementHtml = editor.innerHTML.replace(selectedText, imgElement);
        editor.innerHTML = replacementHtml;
      };
      reader.readAsDataURL(file);
    }
  }
</script>

</body> </html>

Enter your comment:
C J K J O
 

This topic does not exist yet

You've followed a link to a topic that doesn't exist yet. If permissions allow, you may create it by clicking on Create this page.

  • en/text.txt
  • 2023/03/21 10:36
  • brahmantra