Asked  7 Months ago    Answers:  5   Viewed   47 times

output in Chrome:

<div id="content" contenteditable="true" style="border:1px solid #000;width:500px;height:40px;">
    hey
    <div>what's up?</div>
<div>
<button id="insert_caret"></button>

I believe in FF it would look something like this:

hey
<br />
what's up?

and in IE:

hey
<p>what's up?</p>

unfortunately, there is no nice way of making it so that every browser inserts a <br /> instead of a div- or p-tag, or at least I couldn't find anything online.


ANYWAY, what I am trying to do now is, when I hit the button, I want the caret to be set at the end of the text, so it should look something like this:

hey
what's up?|

any way to do this so it works in all browser?

example:

$(document).ready(function()
{
    $('#insert_caret').click(function()
    {
        var ele = $('#content');
        var length = ele.html().length;

        ele.focus();

        //set caret -> end pos
     }
 }

 Answers

31

The following function will do it in all major browsers:

function placeCaretAtEnd(el) {
    el.focus();
    if (typeof window.getSelection != "undefined"
            && typeof document.createRange != "undefined") {
        var range = document.createRange();
        range.selectNodeContents(el);
        range.collapse(false);
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (typeof document.body.createTextRange != "undefined") {
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(el);
        textRange.collapse(false);
        textRange.select();
    }
}

placeCaretAtEnd( document.querySelector('p') );
p{ padding:.5em; border:1px solid black; }
<p contentEditable>foo bar </p>

Placing the caret at the start is almost identical: it just requires changing the Boolean passed into the calls to collapse(). Here's an example that creates functions for placing the caret at the start and at the end:

function createCaretPlacer(atStart) {
    return function(el) {
        el.focus();
        if (typeof window.getSelection != "undefined"
                && typeof document.createRange != "undefined") {
            var range = document.createRange();
            range.selectNodeContents(el);
            range.collapse(atStart);
            var sel = window.getSelection();
            sel.removeAllRanges();
            sel.addRange(range);
        } else if (typeof document.body.createTextRange != "undefined") {
            var textRange = document.body.createTextRange();
            textRange.moveToElementText(el);
            textRange.collapse(atStart);
            textRange.select();
        }
    };
}

var placeCaretAtStart = createCaretPlacer(true);
var placeCaretAtEnd = createCaretPlacer(false);
Tuesday, June 1, 2021
 
akohout
answered 7 Months ago
73

You can set the caret position using CaretIndex property of a TextBox. Please bear in mind that this is not a DependencyProperty. Nevertheless, you may still set it in XAML like this:

<TextBox Text="123" CaretIndex="{x:Static System:Int32.MaxValue}" />

Please remember to set CaretIndex after Text property or else it will not work. Thus it probably won't work if you bind to Text like in your example. In that case, simply use code-behind like this.

NumberOfDigits.CaretIndex = NumberOfDigits.Text.Length;
Thursday, June 10, 2021
 
Saxophlutist
answered 6 Months ago
15

As per the spec:

If the value is translucent, the computed value will be the rgba() corresponding one. If it isn't, it will be the rgb() corresponding one.

Meaning that no matter what is your input, the computed value always results in either rgb or rgba.

So, answering your question: yes, it is standard behaviour and no, you can't use hex or hsl as it will be computed back to rgba.

Saturday, July 31, 2021
 
Pradip
answered 5 Months ago
83

Thank you everyone! Finally, I was able to solve my problem by referencing to the following posts.

android: two issues using Tablerow+TextView in Tablelayout

How to make a TableLayout from XML using programmable way ?

Refer below for my working code.

This is where the dynamic adding of rows is done.

TableLayout tl = (TableLayout)findViewById(R.id.table_layout);

private void addTableRow(int resIdTitle, String strValue){

    LayoutInflater inflater = getLayoutInflater();
    TableRow tr = (TableRow)inflater.inflate(R.layout.table_row, tl, false);

    // Add First Column
    TextView tvTitle = (TextView)tr.findViewById(R.id.tvTitle);
    tvTitle.setText(resIdTitle);

    // Add the 3rd Column
    TextView tvValue = (TextView)tr.findViewById(R.id.tvValue);
    tvValue.setText(strValue);

    tl.addView(tr);
}

This is the table_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <TableLayout
      android:id="@+id/table_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:title="@string/strGetParamTitle" 
      android:scrollbars="vertical"       
      android:shrinkColumns="0">
    </TableLayout>
</ScrollView>

And finally the table_row.xml. I've set all the layout_params here.

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
   <TextView  
          android:id="@+id/tvTitle"
          android:paddingRight="3dip"
          android:layout_width="0px"
          android:layout_weight="0.35"/>
   <TextView  android:text=":"
          android:layout_width="0px"
          android:layout_weight="0.05"/>
   <TextView
            android:id="@+id/tvValue"
            android:paddingLeft="3dip"
            android:layout_width="0px"
            android:layout_weight="0.6"
          /> 
</TableRow>
Sunday, August 15, 2021
 
saad
answered 4 Months ago
63

The best way to check for a certain feature is to actually test for that feature on an element that you know should support it, like this:

if(element.contentEditable != null)
    // then it's supported

or

if(typeof(element.contentEditable) != 'undefined')
   // same thing.. but with typeof you can be more specific about the type of property you need to find
Thursday, September 30, 2021
 
showstealer
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share