SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot samohtwerdna's Avatar
    Join Date
    Jul 2007
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery remove span wrapping input

    hello all,

    I want to use jquery to make sure that my HTML forms have the correct structure and classes.

    What I want:
    HTML Code:
    <form class="myclass">
    <label class="label-class"><span class="there_is_supposed_to_be_a_span_here">Label</label>
    <input class="input-class" type="text" />
    </form>
    So I want to use jquery to add the classes to the elements and also to remove any unwanted spans.


    This is what I have so far:
    Code:
    	jQuery('form').addClass('myclass');
    	jQuery('label').addClass('label-class');
    	jQuery('input:text').addClass('input-class');
    
    //now remove unwanted spans
    var  inp = jQuery("input");
    var  clean = jQuery(inp[0].outerHTML);
    jQuery("span", clean).remove();
    Only problem is that my remove code gets rid of all spans! - I only want to remove spans that are wrapping the inputs or the labels - but I need to leave the span nested inside the label.

    How can I do this??
    Finding beauty in the simple.
    Five Elephants

  2. #2
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Erm, not 100% sure what you are trying to achieve.

    So some labels and inputs are wrapped in spans and some are not? You want to find the ones that are and remove the spans? Is that correct?

    How about putting the relavent html into a variable and removing the spans using a regex? Then appending the variable data to the DOM?


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
  •