SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jan 2009
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change div to span

    Change div to span

    Hi all

    Is it possible to change just the start and closing tag without effecting the content.

    Can I change

    Code:
     <div><img src="image.jpg" alt="image desc." title="image title" /></div>
    to this

    Code:
     <span><img src="image.jpg" alt="image desc." title="image title" /></span>

    The content of the div might be longer so I just want to change the start and elements.

  2. #2
    SitePoint Member
    Join Date
    Apr 2009
    Location
    Ensenada BC Mexico
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can nest it something like
    <div><span><img ...> What ever </span></div>

    But I think your question is kind of vague, Could you explain more What are you try to accomplish?
    There are 10 kinds of persons in this world 1 those who now binary and 10 the rest

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ttmt View Post
    Is it possible to change just the start and closing tag without effecting the content.
    No, it's not possible to change an element in to another one, but what can be done is to move the contents of an element to another one, then remove the original element.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Addict
    Join Date
    Jan 2009
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did what I wanted to do with

    Code:
    $(div).replaceWith($('<span/>').html($(this).html()));

  5. #5
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying this
    Code:
      
    <html>
    <head>
    <script src="http://code.jquery.com/jquery-1.7.1.js"></script>
    </head>
    <body>
    <div><img src="image.jpg" alt="image desc." title="image title" ></div>
    
    <script type="text/javascript">
    
    alert(document.body.innerHTML);  // <div><img ....
    
    // http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_html_replacewith_func
    
    $('div').replaceWith(function() {
    
    return '<span>'+$(this).html()+'</span>';
    
    //return $(this).html();
    });
    
    alert(document.body.innerHTML);  // <span><img ...
    
    </script>
    </body>
    </html>
    This code is working in Firefox 4.0b9


    unwrap, wrap
    Code:
      
    <html>
    <head>
    <script src="http://code.jquery.com/jquery-1.7.1.js"></script>
    </head>
    <body>
    <div><img src="image.jpg" alt="image desc." title="image title" ></div>
    
    <script type="text/javascript">
    // http://www.w3schools.com/jquery/jquery_ref_html.asp
    
    alert(document.body.innerHTML);  // <div><img ....
    
    // http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_html_unwrap
    
    $('img').unwrap();
    
    alert(document.body.innerHTML);  //  <img ....
    
    // http://www.w3schools.com/jquery/html_wrap.asp
    
    $('img').wrap('<span></span>'); 
    
    
    alert(document.body.innerHTML);  // <span><img ...
    
    </script>
    </body>
    </html>
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.


Bookmarks

Posting Permissions

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