Jump to content

set the cursor inside a tag !


lofaifa

Recommended Posts

i want to set the cursor inside a span tag that is inside a div editor when its created and the user complete writing inside the span

 

html :

 

<input type="button" value="<-" onclick="change_direction()" />
<div id="question_text" class="question" contenteditable="true">
<span id="spon"></span>
</div>

 

javascript :

 

function change_direction(){
var string=$('.question').html();
        var empty_jump=string.substr(-4);
var jump=string.substr(-10);
if(jump==='<br></div>'||empty_jump==='<br>'||string===''){
	$(".question").append("<span id='spon'></span>");
	// want to set the cursor here !!!!! this doesnt work
                document.getElementById('spon').focus(); 
                //this also
	$("#spon").focus();
}
else{
	//not changing anything
}
}



Link to comment
Share on other sites

I dont think javascript allows you to move the users cursor, but I may be wrong. But functions like that can really be abused

 

Ever been entering your phone number into 3 text fields, and the focus moves to the next one after 3 digits? I believe to do this you use javascript to set which element has focus.

 

Unless you're talking about the mouse pointer, not the text cursor.

Link to comment
Share on other sites

the problem is not jumping from an input to another

my main problem is changing direction of just the current line u want , the other text stays in the same direction

for example where writing something in english (ltr) and i want to write some arabic (rtl)

i start a new line and when i click a button javascript function create a new tag inside the whole div im writing in with different direction .

the first line was ltr

and in second line when u clicked the cursor change direction and u can write arabic rtl

first line ltr

second line rtl

this is what i want

 

this is the main div tag :

 

<div id="question_text" class="question" contenteditable="true"></div>

 

button to change direction :

 

<input type="button" value="<-" onclick="change_direction()" />

 

the javascript function im trying to play with :

 

function change_direction(){
var string=$('.question').html();
var long=string.length;
var empty_jump=string.substr(long-4);
var jump=string.substr(long-10);
        //to make sure if the user jumped the line before clicking the button
if(jump==='<br></div>'||empty_jump==='<br>'||string===''){
	var ha=string.substr(0,long-15);
                //when i return a new line inside div and display the html a new line means : <div><br></div> so i want it plus span to change direction
	$(".question").html(ha+"<span><div><br></div></span>");
                //now i want the cursor to be inside <span><div></div></span>
	... CODE ...
}
else{

}
}

 

the function is obv bad but i didnt find any alternative !

 

CSS :

 

#question_text span{
direction:ltr;
float:left;
margin-left:15px;
font-family:"Courier New", Courier, monospace;
font-size:14px;
}

 

Default direction is rtl in my case !

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.