For modern browsers you should use:
document.getElementById("myspan").textContent="newtext";
While older browsers may not know textContent
, it is not recommended to use innerHTML
as it introduces an XSS vulnerability when the new text is user input (see other answers below for a more detailed discussion):
//POSSIBLY INSECURE IF NEWTEXT BECOMES A VARIABLE!!
document.getElementById("myspan").innerHTML="newtext";
Related Contents:
- offsetting an html anchor to adjust for fixed header [duplicate]
- How can I know which radio button is selected via jQuery?
- Using jQuery to center a DIV on the screen
- In what situations would AJAX long/short polling be preferred over HTML5 WebSockets?
- Convert json data to a html table [closed]
- Is it wrong to place the tag after the tag?
- Converting HTML string into DOM elements?
- How can javascript upload a blob?
- Resize image with javascript canvas (smoothly)
- Refresh/reload the content in Div using jquery/ajax
- JavaScript Form Submit – Confirm or Cancel Submission Dialog Box
- How to send a JSON object using html form data
- ReferenceError: event is not defined error in Firefox
- How can I get the values of data attributes in JavaScript code?
- What is offsetHeight, clientHeight, scrollHeight?
- Selecting all text in HTML text input when clicked
- Understanding HTML Form Element behavior
- How to close an open collapsed navbar when clicking outside of the navbar element in Bootstrap 3?
- How to make Internet Explorer emulate pointer-events:none?
- How can I detect whether an iframe is loaded?
- Creating a jQuery object from a big HTML-string
- How To Determine Which Submit Button Was Pressed, Form onSubmit Event, Without jQuery [duplicate]
- Performance differences between visibility:hidden and display:none
- What’s standard behavior when element is clicked? Will it submit the form?
- Setting CSS value limits of the window scrolling animation
- jQuery (almost) equivalent of PHP’s strip_tags()
- Detecting that the browser has no mouse and is touch-only
- Is it possible to programmatically detect size limit for data url?
- Open a new javascript window(.open) along with its CSS styling
- Avoid window jump to top when clicking #-links
- How to ONLY trigger parent click event when a child is clicked
- Error message “DevTools failed to load SourceMap: Could not load content for chrome-extension://…”
- How to disable submit button once it has been clicked?
- How to draw a line between two divs?
- Animated gif only loops once in Chrome and Firefox
- Download Canvas as PNG in fabric.js giving network Error
- Change the content of a div based on selection from dropdown menu
- How to get a leaflet map canvas to have a 100% height?
- Load event not fired on Safari when reloading page
- Infinite scrolling with React JS
- Change iframe src by clicking a link
- Failed to load resource: the server responded with a status of 404 (Not Found)
- Can you use both the async and defer attributes on a HTML tag?
- For a push notification, is a websocket mandatory?
- Sharing websocket across browser tabs?
- How often does the timeupdate event fire for an html5 video
- Adding onClick event dynamically using jQuery
- Drag-and-drop file upload in Google Chrome/Chromium and Safari?
- Drawing arrows on an HTML page to visualize semantic links between textual spans
- Prevent iOS bounce without disabling scroll ability