Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts

    jQuery: adding line breaks which can be similar to nl2br() in PHP?

    hi,
    I wonder if jquery or javascript has the function adding line breaks which can be similar to nl2br() in PHP?

    This is the info i want to grab from a database, PHP will add line breaks (<br/>) when it is passed into nl2br(),

    Code:
    <?php echo nl2br($row_cover['img_info']);?>
    [HTML]Blue Bar
    2007
    Acrylic and digital print on canvas in carved wood, coconut, mother of pearl and coin inlaid artist frame
    72 x 86 x 7 in. (182.9 x 218.4 x 17.8 cm)
    Courtesy Lehmann Maupin Gallery, New York and Jay Jopling, White Cube, London [/HTML]

    PHP Code:
    <?php
    if(empty($pg_id))
        {
        
    $img_id $_REQUEST['img_id'];
        include(
    '../incl_corefuncs/conn_mysql.inc.php');
        include(
    '../incl_corefuncs/core.php');
        }
    ?>
    <?php
    $sql_cover 
    "
                SELECT *, 
                UNIX_TIMESTAMP( img_created ) AS img_created
                FROM root_images
                WHERE root_images.pg_id = '$pg_id'
                OR root_images.img_id = '$img_id'
                "
    ;
    //
    $result_cover mysql_query($sql_cover);
    $row_cover mysql_fetch_assoc($result_cover);
    ?>
                
    <!--cover-->
    <div class="cover">
        <a href="#"><img src="<?php echo $row_cover['img_path_large'];?>" alt="<?php echo $row_cover['img_name'];?>"/></a>
    </div>
    <!--cover-->

    <div class="info">
      <?php echo nl2br($row_cover['img_info']);?>
    </div>


    <?php
    $sql_preview 
    "
                SELECT *, 
                UNIX_TIMESTAMP( img_created ) AS img_created
                FROM root_images
                WHERE root_images.pg_id = '$pg_id'
                "
    ;
    //
    $result_preview mysql_query($sql_preview);
    ?>
                
    <!--preview-->
    <div class="preview">
        <ul>
        <?php
        
    while($row_preview mysql_fetch_assoc($result_preview)) 
            {
            list(
    $width$height$type$attr) = getimagesize($row_preview['img_path_large']);
        
    ?>
            <li><a href="<?php echo $row_preview['img_id'];?>" onclick="loadImage('<?php echo $row_preview['img_id'];?>',<?php echo $height;?>); return false;"><img src="<?php echo $row_preview['img_path_small'];?>" alt="<?php echo $row_preview['img_name'];?>" longdesc="<?php echo $row_preview['img_info'];?>"/></a></li>
        <?php
            
    }
        
    ?>
        </ul>
    </div>
    <!--preview-->
    the trouble is, i can grab the data and put it in the div with jquery, but i dont know how to add line breaks in the data... this is the code,

    Code:
    <script type="text/javascript" language="javascript">
    // <![CDATA[
    $(document).ready(function(){
    $("#imagepreview li:first").addClass("currentimage");
    });
    
    // remove all classes
    function removeClassAll() {
    	$("#imagepreview li").removeClass("currentimage");
    	}
    	
    // load content with ajax and add a class
    function loadImage(img_id, height) {
    
    $("#imagepreview li").click(function () {
    	removeClassAll();
    	$(this).addClass("currentimage");
    	var $this = $(this);
    	var info = $this.find('img').attr("longdesc");
    	$(".info").text(info);
        });
    
    $("#imagecover").fadeOut('fast', function(){
    	$("#imagecover").html('<div class="ajaxloader"><p><img src="img_iconies/loader_0.gif"/> loading...</p></div>');
    });
    
    $("#imagecover").animate({height:height}, 500, function(){
    	$("#imagecover").load( "incl_layouts.global/item.projects.images.cover.php?img_id=" + img_id, {}, function(){ //calling the ajax then with a callback afterwards
    		$("#imagecover").css({display:'none'})
    		.fadeIn("slow");	
    		});
    	});	
    }
    // ]]>
    </script>
    thanks if u can help.

    Lau

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You are using an Ajax call to get the data which is calling a PHP script to actually get the data so why not use the nl2br() function while in the PHP script so the data comes back from the Ajax call with the BR tags already in place?

  • #3
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    You are using an Ajax call to get the data which is calling a PHP script to actually get the data so why not use the nl2br() function while in the PHP script so the data comes back from the Ajax call with the BR tags already in place?
    yes i just have used the nl2br() function in the longdesc and then change this line,

    Code:
    $(".info").html(info);
    to

    Code:
    $(".info").html(info);
    its now perfect! thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •