rain13 Posted April 10, 2011 Share Posted April 10, 2011 I found that when I select only part of text and then click some button, then it puts tags around selected area, but if all text is selected (ctrl +a) then buttons to wrap tags around doesnt work at all. does anyone know what might cause this? <html><head><title>PHP using AJAX</title><script type="text/javascript">var time_variable;function getval(){return "abc";}function getXMLObject() //XML OBJECT{ var xmlHttp = false; try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+ } catch (e2) { xmlHttp = false // No Browser accepts the XMLHTTP Object then false } } if (!xmlHttp && typeof XMLHttpRequest != 'undefined') { xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers } return xmlHttp; // Mandatory Statement returning the ajax object created}var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax objectfunction ajaxFunction() { var getdate = new Date(); //Used to prevent caching during ajax call if(xmlhttp) { var txtname = document.getElementById("Editor"); xmlhttp.open("POST","post.php?a=preview&t=0&f=0",true); //calling testing.php using POST method xmlhttp.onreadystatechange = handleServerResponse; xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xmlhttp.send("Editor=" + txtname.value); //Posting txtname to PHP File }}function handleServerResponse() { if (xmlhttp.readyState == 4) { if(xmlhttp.status == 200) { document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element } else { alert("Error during AJAX call. Please try again"); } }}function set(style) { var StyleStart = "["+style+"]"; var StyleEnd = "[/"+style+"]"; if (style == "youtube") { var StyleStart = "[video host=youtube.com]"; var StyleEnd = "[/video]"; } if (style == "google") { var StyleStart = "[video host=google.com]"; var StyleEnd = "[/video]"; } var ta = document.getElementById("Editor"); if (document.selection) { str = document.selection.createRange().text document.selection.createRange().text = StyleStart + str + StyleEnd; return true; } else if (ta.selectionStart) { var startPos = ta.selectionStart; var endPos = ta.selectionEnd; var str = ta.value.substring(startPos, endPos); ta.value = ta.value.substring(0, startPos) + StyleStart + str + StyleEnd + ta.value.substring(endPos, ta.value.length); return true; } else { return false; }}function DisHex(str){ var r=''; for(var i=0;i<str.length;i+=2) { r+=unescape('%'+str.substr(i,2)); } return r;}</script><div id="message" name="message"></div><br><input type="button" value="bold" onclick="set('b')" /> <input type="button" value="Italic" onclick="set('i')" /> <input type="button" value="Underline" onclick="set('u')" /> <input type="button" value="Image" onclick="set('img')" /> <input type="button" value="Link" onclick="set('lnk')" /> <input type="button" value="Quote" onclick="set('quote')" /> <input type="button" value="Youtube" onclick="set('youtube')" /> <input type="button" value="Google video" onclick="set('google')" /><br /><textarea id="Editor" style="height:300px; width:76%"></textarea><br><script type="text/javascript">var txtArea = document.getElementById('Editor');var ContentStr = DisHex("");replaced = ContentStr.replace(/<br>/g, "\n");txtArea.value = replaced;</script><input type="button" value="Preview" onclick="ajaxFunction()" /><input type="submit" value="Submit" /> Link to comment Share on other sites More sharing options...
rain13 Posted April 13, 2011 Author Share Posted April 13, 2011 does anyone know what causes this? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 13, 2011 Share Posted April 13, 2011 Add some debugging to the set function to see what the values of the different properties and things are. If ta.selectionStart is 0, meaning the selection starts at the beginning of the text, then you probably want to use that. Right now your code is skipping it if the selection starts at the beginning. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.