JavaScript - Please Help Me To: Get All Char From Each Line Of Div
sub:
Please help me to get all characters from every line from a word-wrapped div using javascript. Detail: Need to get all the characters for first line, second line, third line,... separately from the word-wrapped div Thanks for your reply. Similar TutorialsI'm using jQuery+drupal and some jQuery plugins. All is ok with Firefox. But in IE's i'm receiving problem like this. Line: 1 Char: 1 Error: object expected URL: http://businessway.am I have included .js files. How to know where is the problem? In which file? Line 1? Which file? If you can please check with this address: http://businessway.am Is there a way to calculate the .offsetLeft or .left of a character in a string relative the element that contains the string? I'm just trying to wrap each character in an element and position the characters independently, so I have to set the .position to absolute, and set the .left and .top on each element as I create it so I can move the elements later on. Thanks for any help you might have for me. I was looking at that old Strawberry Fields problem and I thought I'd see about solving it in JavaScript. What I want is an array of chars so that I can set individual elements in the array of arrays. Strings are apparently immutable and can't directly be changed? Here's my code. Code: var field = new Array(); var string0 = "..@@@@@..............." var string1 = "..@@@@@@........@@@..." var string2 = ".....@@@@@......@@@..." var string3 = ".......@@@@@@@@@@@@..." var string4 = ".........@@@@@........" var string5 = ".........@@@@@........" for (var i = 0; i < 6; i++) { field[i] = new Array(); field[i] = eval("string" + i + ".slice('')"); } document.write("field's type is " + typeof field + "<br>"); // object? but it should be explicit array document.write(typeof field[1]) // string? it should explicitly be an array, then it was filled with array elements document.write(typeof field[1][2]); // string - ok, I understand this bit document.write(field[1].length); document.write("<br>"); for (var i = 0; i < field.length; i++) { field[i] = new Array(); for (var k = 0; k < eval("string" + i + ".length"); k++) { field[i][k] = eval("string" + i + ".charAt(" + k + ")"); } document.write("<br>"); } document.write("field's type is " + typeof field + "<br>"); // seriously, an object? document.write(typeof field[1]) // why is this an object instead of an array? document.write(typeof field[1][2]); // string, yeah, I understand this as well document.write(field[1].length); document.write("<br>"); Also, it looks like I can't overwrite a 1-length string that's in the array of arrays. For instance: Code: newField = field; //for loops newField[i][k] = 0; // does nothing, newField's elements remain the same. How do I get new line to work in javascript. Everytime I try to use it, it never works. There should be a new line with 5 * instead of having 17 * on one line. Code: <html> <body> <script type ="text/javascript"> var x=0; for (x=0;x<=12;x++) { document.writeln("*"); } document.writeln("\n"); for (x=0;x<=5;x++){ document.writeln("*"); } </script> </body> <html> I am trying to implement a javascript on a page and the error that I get is Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /fullsize/image.php on line 131 On line 131 the code is Code: echo"<img id='thepic' onClick="scaleImg('thepic')" onLoad="scaleImg('thepic')" src='$image'>"; hi, I am tring to display some texts in a textbox using \n to create a new line but it does not work. PHP Code: <script> document.getElementById("txtbox1").value=" this is my firstline \n This is my second line \n this is my third line" </script> <input type="text" id="txtbox1"> thanks the HTML code: Code: <ul id="ul1"> <li>1111</li> <li>2222</li> </ul> <div id="div1"></div> the javascript code: Code: var oDiv=document.getElementById('div1'); oDiv.style.left=getPos(this).left+this.offsetWidth+'px'; oDiv.style.top=getPos(this).top+'px'; function getPos(obj){ var aPos={left: null, top: null}; while(obj) { aPos.left+=obj.offsetLeft; aPos.top+=obj.offsetTop; obj=obj.offsetParent; } return aPos; } why it write the while loop,and what's effection of the while loop obj=obj.offsetParent; what's this line meaning? I have these line of javascript: Code: $(document).ready(function () { /*$('#first').focus(); $(this).keyup(function (e) { if (e.keyCode == 13) { generate(); } });*/ }); function generate() { if($("input[name='name']").val().length > 1) { fi = $("input[name='name']").val().substring(0,1).toUpperCase(); var sl = ["Cool "]; var sl2 = ["Hand "]; var sl3 = ["Luke",]; { var i = Math.floor(Math.random() * sl.length); var j = Math.floor(Math.random() * sl2.length); var k = Math.floor(Math.random() * sl3.length); var nick = sl[i] + sl2[j] + sl3[k]; } { $("#answer").text("Your Name Is: " +nick); $("#answer").fadeIn("slow"); } ; } } I would like for it to return: Your Name Is: Cool Hand Luke I want the "Cool Hand Luke" to be on a separate line but I don't know how to put a linebreak in the .text string. Is this something somebody could tell me how to do? Where in the line: Code: $("#answer").text("Your Name Is: " +nick); Do I put (<br>) I know you can add data to array wiht the push() method but can you add a whole new line as in a = new Array a [1] [1,2,3] so it would create a new line a [2] [4,5,6]; The syntax to the array i think is wrong but you get the question. Hello: First post here on CS ... I am wondering why my code will not write a new line or line break. This is fine: Code: <script> document.write("Hello and welcome to javascript!"); </script> But when I try to put a message on two lines, it does not work. The book I'm reading and several websites have stated the below codes will work, but neither do: Code: <script> document.write("Hello and welcome to javascript! \n"); document.write("It is a tricky language to learn!") </script> Code: <script> document.writeln("Hello and welcome to javascript!"); document.writeln("It is a tricky language to learn!") </script> Am I missing something? Hi, I'm creating a new email in javascript via outlook. When i try entering a line break "\n", "\r", "\r\r", etc, Outlook just ignores it completely. I've tried using "%0D%0A" and "fromCharCode(10)" as well but it doesnt seem to read it as a line break. Is there any way to code it so that a line break or a carriage return appears on outlook? Hi, I'm trying to do an assignment, and have copied a piece of code to learn while I do, if you see what I mean. Problem is, I'm having real trouble with one of the lines of code. If someone could tell me basically what it does, I'd be very greatful! Here's the code: if (height[i] == -(array[i].height)) The - before (array[i].height) is really confusing me! Thanks in advance! Is there any way to have a multiline text string in javascript? For example strname="line one of string line two of string"; I get an error that the string is not terminated. The reason I want to do this is from the following code: Code: <textarea name="comments" id="comments" cols="90" rows="8" ></textarea> <?php if (isset($_REQUEST["registered"])) { $end_of_line=chr(10); $carriage_return=chr(13); $step1=str_replace($carriage_return,"",$comments); $step2=str_replace($end_of_line,"<br>",$step1); echo "<script type='text/javascript'>\n"; echo "intermediate='",$step2,"';\n"; echo "comment_value=",'intermediate.replace(/<br>/g,"\n")',";\n"; echo "document.getElementById('comments').value=comment_value;</script>\n"; }?> I an getting the value to set a textarea to from the server. It may have newline characters. If there is a way to have a multiline string, then I could skip all the steps and just echo one javascriopt command. Can this be done? CraigRS I have this Javascript and I am wanting to create a line break between the tweet and the time it was posted, here is the javascript, I am guessing I need to insert the <BR> tag somewhere but just not sure where? Thanks function twitterCallback2(twitters) { var statusHTML = []; for (var i=0; i<twitters.length; i++){ var username = twitters[i].user.screen_name; var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) { return '<a href="'+url+'">'+url+'</a>'; }).replace(/\B@([_a-z0-9]+)/ig, function(reply) { return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>'; }); statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id_str+'">'+relative_time(twitters[i].created_at)+'</a></li>'); } document.getElementById('twitter_update_list').innerHTML = statusHTML.join(''); } function relative_time(time_value) { var values = time_value.split(" "); time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3]; var parsed_date = Date.parse(time_value); var relative_to = (arguments.length > 1) ? arguments[1] : new Date(); var delta = parseInt((relative_to.getTime() - parsed_date) / 1000); delta = delta + (relative_to.getTimezoneOffset() * 60); if (delta < 60) { return 'less than a minute ago'; } else if(delta < 120) { return '<br>about a minute ago'; } else if(delta < (60*60)) { return (parseInt(delta / 60)).toString() + ' minutes ago'; } else if(delta < (120*60)) { return 'about an hour ago'; } else if(delta < (24*60*60)) { return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago'; } else if(delta < (48*60*60)) { return '1 day ago'; } else { return (parseInt(delta / 86400)).toString() + ' days ago'; } } I have a slideshow that goes through pictures once you click on the Play button and doesn't stop until you press on the Stop button. How can I make it so that it automatically plays once the website loads without having to click on the Play button? Heres the code: Code: <div class="tabs png"><span class="tabs-inner"><a href=""></a> <a href=""></a> <a href=""></a> <a href=""></a> <a href=""></a> <a href=""></a> <a href=""></a></span></div> <div class="slider-indent-button"> <a href="#" onClick='$("div.tabs").tabs().play();'>Play</a> <a href="#" onClick='$("div.tabs").tabs().stop();'>Stop</a> </div> All help is much appreciated!!! Hey guys. I'm working on a "Preview Post" function. When the Preview Post button is clicked, it should reload the page, show the preview above the textarea, then send the text back to the textarea so the user can keep working. It works until I move to a second line. If there are line breaks they don't get shown in the preview. Also, the text isn't sent back to the textarea. I have a pair of functions that are supposed to handle the input string. The first function records the input when typing, and the second function picks up the post to print as a preview: You can see what I mean he Code: function preview() { var preview = window.document.postform.inputpost.value; window.document.hidden.inputpreview.value = preview; } function copypreview() { var copypreview = '<?php echo $_POST[inputpreview]; ?>'; window.document.postform.inputpost.value = copypreview; } Here's how I use them: Code: <body onload='copypreview();'> <?php include("navbar.php"); ?> <?php if ($_GET[preview]) { echo "<table id=blog><tr><td>"; echo $_POST[inputpreview]; echo "</td></tr></table>"; } ?> <form name="postform" action="submitpost.php" method="post"> Post Title <input size="40" type="text" name="inputtitle" id="inputtitle"> <?php include("tagbar.php"); ?> <textarea onchange="preview();" cols="101" rows="20" id="inputpost" name="inputpost"></textarea> <div align=center> <input type="submit" value="Submit"/> </form> <form name="hidden" method="post" action="addpost.php?preview=1"> <input type="hidden" name="inputpreview"> <input type="submit" onClick="preview();" value="Preview Post"/> </form> Sorry I know it's a lot of code. I tried to make it as readable as possible. Can someone tell me how to fix this? I thought I might have to find a way to insert <br> tags when the user hits the Enter key, but I could have sworn this thing was working with line breaks before. I have a piece of code that randomly picks a line from a preset list and displays it whenever the page is loaded. I'm using it to generate random quotes on my pages. At the moment, there seems to be a good chance that when the page loads no quote appears at all, just a blank space. Anyone know why this could be happening? the code I am using is: Code: <script type="text/javascript"> q0= "<img src=\"images/quotes/01.png\" />"; q1= "<img src=\"images/quotes/02.png\" />"; q2= "<img src=\"images/quotes/03.png\" />"; q3= "<img src=\"images/quotes/04.png\" />"; q4= "<img src=\"images/quotes/05.png\" />"; q5= "<img src=\"images/quotes/06.png\" />"; q6= "<img src=\"images/quotes/07.png\" />"; q7= "<img src=\"images/quotes/08.png\" />"; q8= "<img src=\"images/quotes/09.png\" />"; q9= "<img src=\"images/quotes/10.png\" />"; q10= "<img src=\"images/quotes/11.png\" />"; q11= "<img src=\"images/quotes/12.png\" />"; var ran_unrounded=Math.random()*11; var ran_number=Math.round(ran_unrounded); document.write(eval("q"+ran_number)); </script> Hi everyone I am trying to complete some code involving a while loop. It's actually part of an assignment. Although the code is outputting ALMOST correctly there is one thing bugging me. Here is the code: Code: playersScore = rollDie(); document.write(' Sco ' + playersScore); playersPosition = playersScore + playersPosition; document.write(', Squa ' + playersPosition); indexOfNumber = findIndexOf(playersPosition, specialSquaresArray); if (indexOfNumber != -1) { document.write(', Ladder to Squa ' + connectedSquaresArray[indexOfNumber] + '<BR>'); playersPosition = connectedSquaresArray[indexOfNumber]; indexOfNumber = -1; while(playersPosition<80) { playersScore = rollDie() document.write(' Sco ' + playersScore) playersPosition = playersPosition + playersScore document.write(', Squa ' + playersPosition) indexOfNumber = findIndexOf(playersPosition, specialSquaresArray) if(indexOfNumber != -1) { document.write(', Ladder to Squa ' + connectedSquaresArray[indexOfNumber]); playersPosition = connectedSquaresArray[indexOfNumber]; } document.write('<BR>'); Now, from what I can see I think it's correct. But if if(indexOfNumber != -1) is not executed I get the following: Sco 2, Squa 2 Sco 4, Squa 6 OK Sco 4, Squa 6 should be on a new line below Sco 2, Squa 2 but no matter where I add the line break, it just will not do it. Can somebody please point me in the right direction as I am wasting so much time on this silly little problem Many thanks in advance EDIT: Ok after further fiddling with the code I entered document.write('<BR>'); in the required place and removed + '<BR>' from document.write(', Ladder to Squa ' + connectedSquaresArray[indexOfNumber] + '<BR>'); This has fixed the problem so I'm happy now. Sorry if I wasted anybodies time. I can't believe I have spent over 7 hours trying to figure this out! How do I change the <br /> tags in the follow code to javascript code that performs the same function. I need new lines where you see the <br /> tags and I can't use <br /> since my javascript is within the header section of my HTML file and the W3C validator doesn't like it. Therefore, how do I change the following code to pure javascript with no <br />'s. I tried using \n and \r\n but maybe I didn't use this right as it didn't work. Please help, I am new to javascript txt="Name: "+name+"Sex: "+sex+"<br />Ethnicity: "+ethnicity+"<br />Region: "+region+"<br />Profession: "+profession+"<br />Membership_status: "+membership_status+"<br />Seeking: "+seeking+"<br />Education: "+education+"<br />Body Type: "+body_type+"<br />Eye Colour: "+eye_colour+"<br />Smoker: "+smoker+"<br />Drinker: "+drinker ; |