Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

change style selected text js

function changeFont(font) {
  var sel = window.getSelection(); // Gets selection
  if (sel.rangeCount) {
    // Creates a new element, and insert the selected text with the chosen font inside
    var e = document.createElement('span');
    e.style = 'font-family:' + font.value + ';'; 
    e.innerHTML = sel.toString();

    // https://developer.mozilla.org/en-US/docs/Web/API/Selection/getRangeAt
    var range = sel.getRangeAt(0);
    range.deleteContents(); // Deletes selected text…
    range.insertNode(e); // … and inserts the new element at its place
  }
}
Comment

change style selected text js

Highlight text and change font <br>
<select id="select_font" onchange="changeFont(this);">
            <option value="Arial">Arial</option>
            <option value="Sans Serif" selected>Sans Serif</option>
            <option value="Comic Sans MS">Comic Sans MS</option>
            <option value="Times New Roman">Times New Roman</option>
            <option value="Courier New">Courier New</option>
            <option value="Verdana">Verdana</option>
            <option value="Trebuchet MS">Trebuchet MS</option>
            <option value="Arial Black">Arial Black</option>
            <option value="Impact">Impact</option>
            <option value="Bookman">Bookman</option>
            <option value="Garamond">Garamond</option>
            <option value="Palatino">Palatino</option>
            <option value="Georgia">Georgia</option>
        </select>
<div contenteditable="true" id="note_header" style="width:200px; height:200px; border: 1px solid #ccc">
  Some Content
</div>
Comment

change style selected text js

function changeFont(font) {
  document.getElementById("note_header").style.fontFamily = font.value;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: setstate too slow 
Javascript :: Quitar objetos duplicados 
Javascript :: JavaScript detect card type 
Javascript :: "date change error" 
Javascript :: jsondb 
Javascript :: Automatically Refresh or Reload a Page using http-equiv 
Javascript :: Custom usePagination hook example 
Javascript :: const and let keywords in ES6 
Javascript :: on click disable esc button using jquery 
Javascript :: hide header in next js page 
Javascript :: image opacity reduce js 
Javascript :: isFinite(): returns true if the number is not Infinity or -Infinity 
Javascript :: iteration methods 
Javascript :: maxscript create new Layer 
Javascript :: react keydown event listener freecodecamp 
Javascript :: date javascript only show day month year 
Javascript :: jshack1 
Javascript :: netlify not deploying react site 
Javascript :: render(<App /); const linkElement = screen.getByText(/learn react/i); expect(linkElement).toBeInTheDocument(); 
Javascript :: react avoid spreading non-dom props across component 
Javascript :: adding items to extjs container 
Javascript :: nestjs mongoose ClassSerializerInterceptor not working 
Javascript :: diable input javascript 
Javascript :: take site to top after clicking in react 
Javascript :: javascript shorthand ternary 
Javascript :: js how to remove blue while click 
Javascript :: visio prevent arrows from snapping 
Javascript :: react native Stack Navigation Prop unused variable 
Javascript :: React Hook "useState" is called in function "cardState" which is neither a React function component or a custom React Hook function 
Javascript :: npm i resulting in many ERESOLVE issues 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =