www.webdeveloper.com
Results 1 to 3 of 3

Thread: Change div to span

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    124

    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
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,670
    you can save the innerHTML of the div in some variable, then remove the div and insert a span using the saved content as its innerHTML

  3. #3
    Join Date
    Jan 2011
    Posts
    123
    You can use a superior to DOM DHTML methods:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <style>
    span{background: yellow; border: solid 1px red;}
    </style>
    </head>
    <body>
    
    <div onclick="switchTag(this)">
    	<img src="image.jpg" alt="image desc." title="image title">
    </div>
    
    <script>
    function switchTag(x){ 
    	x.outerHTML="<span>"+x.innerHTML+"<\/span>";
    	}
    </script>
    </body>
    </html>
    p.s.: your div doesn't have an Id so I'm calling the function inline -otherwise I wouldn't.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles