lofaifa Posted March 28, 2012 Share Posted March 28, 2012 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/259852-set-the-cursor-inside-a-tag/ Share on other sites More sharing options...
UrbanDweller Posted March 28, 2012 Share Posted March 28, 2012 I dont think javascript allows you to move the users cursor, but I may be wrong. But functions like that can really be abused Quote Link to comment https://forums.phpfreaks.com/topic/259852-set-the-cursor-inside-a-tag/#findComment-1331830 Share on other sites More sharing options...
Who8MyFish Posted March 28, 2012 Share Posted March 28, 2012 Pretty sure you can do this with visual basic but not JS. I feel like if this were possible we'd all have Live Jasmin / Google ad sense walpapers on our desktop hahaha. Quote Link to comment https://forums.phpfreaks.com/topic/259852-set-the-cursor-inside-a-tag/#findComment-1331955 Share on other sites More sharing options...
Jessica Posted March 28, 2012 Share Posted March 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259852-set-the-cursor-inside-a-tag/#findComment-1331961 Share on other sites More sharing options...
lofaifa Posted March 28, 2012 Author Share Posted March 28, 2012 maybe i should give up trying to change direction of writing inside a div tag . three days with no result Quote Link to comment https://forums.phpfreaks.com/topic/259852-set-the-cursor-inside-a-tag/#findComment-1331973 Share on other sites More sharing options...
Who8MyFish Posted March 28, 2012 Share Posted March 28, 2012 ohhhhh. I assumed the OP was asking about the XY of the mouse. Moving the cursor is super possible. http://www.htmlgoodies.com/beyond/javascript/article.php/3471131/Jump-Focus-with-Form-Elements.htm Quote Link to comment https://forums.phpfreaks.com/topic/259852-set-the-cursor-inside-a-tag/#findComment-1331974 Share on other sites More sharing options...
lofaifa Posted March 28, 2012 Author Share Posted March 28, 2012 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/259852-set-the-cursor-inside-a-tag/#findComment-1331981 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.