to replace characters into an imput

J

jomla

Hi at all

having an imput text like:

<input type="text" name="text1" id="text1" size=40 />

I want to unsert into this field some characters from js at cursor position
or replacing a lot (one or more) selected characters.

I tryed a js like:

function insert(id,str)

{

var range;

if(document.all)

{

var cDoc= document.getElementById(id);

range=cDoc.selection.createRange();

range.text=str;

} else if (window.getSelection)

cWin = document.getElementById(id);

userSelection = cWin.getSelection();

userSelection=str;

cWin.document.execCommand('insertHTML',false,userSelection);

}

}

id parameter is <input id>

str is character to insert

This js work with a <textarea> but not into a <input>

Thanks for any answer
 
A

Andy

jomla said:
Hi at all

having an imput text like:

<input type="text" name="text1" id="text1" size=40 />

I want to unsert into this field some characters from js at cursor
position or replacing a lot (one or more) selected characters.

I tryed a js like:

function insert(id,str)

{

var range;

if(document.all)

{

var cDoc= document.getElementById(id);

range=cDoc.selection.createRange();

range.text=str;

} else if (window.getSelection)

cWin = document.getElementById(id);

userSelection = cWin.getSelection();

userSelection=str;

cWin.document.execCommand('insertHTML',false,userSelection);

}

}

id parameter is <input id>

str is character to insert

This js work with a <textarea> but not into a <input>

Thanks for any answer


Here's a little mock up example for you...


<html>
<head>
<title>test</title>

<script>
function test(){
var str = "Insert this text";
document.getElementById('text1').value=str;
}
</script>

</head>
<body>

<input type="text" name="text1" id="text1" size=40 />

<a href="javascript: test()">TEST</a>

</body>
</html>
 
J

jomla

"Andy"

wrote
Here's a little mock up example for you...


<html>
<head>
<title>test</title>

yes but it do not replace slected text or it do not add text at cursor
position but it smply replace full value

I'ld like that it replace single characters in place of selected text ot add
text at cursor position
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top