Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9168591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:39:40+00:00 2026-06-17T15:39:40+00:00

I’m currently using a WebView which is editable, and offering controls to change the

  • 0

I’m currently using a WebView which is editable, and offering controls to change the selected text to <H1> ~ <H6>. The default normal style text is just a <div>
I wanted to know, how I might use javascript to check what type of style (H1~H6 or DIV) the text corresponds to…

I’m currently using the following code to modify the text style of the selected text into a <H1>

[webView stringByEvaluatingJavaScriptFromString:
                @"document.execCommand(\"FormatBlock\", false, \"<H1>\")"];

Is there a way to check what style the text that is selected(whether it be by dragging, or just by the location of the cursor) is using javascript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:39:42+00:00Added an answer on June 17, 2026 at 3:39 pm

    A selection in HTML content may span more than one element. It may also consist of only part of the text within the start and end elements. This makes it difficult to provide an exact answer to your question since in the general case there is not a single element name or CSS style applied to a selection. However, hopefully the building blocks mentioned below will allow you to construct behavior that matches your intent.

    Since you’re working with the Objective-C API of WebKit I’ll describe how this works both for the Objective-C API and from within JavaScript. Using the Objective-C API can be more pleasant due to the compiler feedback that you’d not get with JavaScript.


    The selection within a WebView is represented as a DOMRange and can be retreived using -[WebView selectedDOMRange]. The selection starts in the element returned by -startContainer at offset -startOffset in to the text of the node. The selection continues through all nodes in DOM order until offset -endOffset within the node returned by -endContainer.

    An insertion point is represented as a DOMRange that is collapsed, as indicated by the collapsed property. In this case -startContainer and -endContainer should return the same values indicating the node in which the caret is, and -startOffset and -endOffset will be equal and represent the offset in to the text content at which the caret is.

    Once you’ve determined the nodes involved you can use the regular DOM APIs to determine the tag name (-[DOMNode tagName]) and CSS Object Model APIs to retrieve style information (-[DOMDocument getComputedStyle:pseudoElement:]).


    From the JavaScript side, the concepts are similar but there’s an intermediate step of a Selection object before you get to the Ranges.

    You use window.getSelection() to retrieve a Selection object. This object will contain zero or more ranges, each representing a selected region. Zero ranges means there is an insertion point and not a real selection. More than one range is allowed as discontiguous selection is supported on some platforms.

    As noted above, the Selection object will have a rangeCount of zero and an isCollapsed value of true to indicate that the selection is simply an insertion point.

    When the rangeCount is non-zero, you can iterate over the ranges within the Selection by calling getRangeAt repeatedly. From that point you’re back to working with Range objects that have the same interface as the DOMRange objects described in the Objective-C case above, with startContainer, endContainer, startOffset and endOffset properties.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.