Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:04:48+00:00 2026-06-06T21:04:48+00:00

I need to alter some HTML using jQuery – (I have no control over

  • 0

I need to alter some HTML using jQuery – (I have no control over the HTML hence I need to use something client side to make this slight alteration)

If a <br /> is the first element in a div named ‘column’, then I need to remove it.

So:

<div class="column">
   <br />text here lorem ipsum blah<br />

Would become:

<div class="column">
   text here lorem ipsum blah<br />

Note, I don’t want to get rid of all the <br /> tags, only if a <br /> tag directly follows the opening tag.

I had hoped something like this would work, but no joy

$('<div class="column"><br />').replaceWith('<div class="column">');

Any help appreciated! Many thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:04:49+00:00Added an answer on June 6, 2026 at 9:04 pm

    Something like this should do it :

    var pattern = /^<br\/>/;
    $("<div.column").each(function() {
        var $this = $(this),
            text = $this.text();
        if(text.match(pattern)) {
            $this.text(text.replace('<br/>', ''))
        }
    });
    

    (ignore – left in place so as to make sense of comments below)

    EDIT

    Try this :

    var pattern = /^\n*\s*<br>/;
    $("div.column").each(function() {
        var $this = $(this),
            html = $this.html();
        if(html.match(pattern)) {
            $this.html(html.replace(pattern, ''))
        }
    });
    

    DEMO

    As @minitech points out, any event handlers and data attached to the original HTML will be lost, so either :

    • do the replacement before attaching any event handlers/data
    • take measures to re-instantiate event handlers/data after replacement
    • delegate event handling to the container element
    • do something completely different that is non-destructive – see @minitech’s answer.

    Second EDIT

    After much playing, at last something concise. Try this near 100% jQuery version of @minitech’s approach :

    $('.column').each(function() {
        $.each(this.childNodes, function(i, c) {
            return !$(c).filter('br').remove().end().text().trim(); 
        });
    });
    

    DEMO

    Explanation: The inner loop visits each childNode in turn; its single statement removes the current node if it is a <br> but allows the loop to progress only if the current node is blank or whitespace. Note judicious use of .end() to keep everything in one method chain.

    Efficiency: Poor – that jQuery method chain must consume a few CPU cycles but that seems a small price to pay.

    Readabiity: Close to nada.

    Third EDIT

    With a mild mod, this will handle any combination of leading whitespace/BRs/HTML comments :

    $('.column').each(function() {
        $(this.childNodes).each(function(i, c) {
            return !$(c).filter('br').remove().end().text().trim(); 
        });
    });
    

    The difference from the last version is that the jQuery object $(this.childNodes) remains unaffected by node removal, whereas the raw this.childNodes is affected and the .each() loop doesn’t scan properly. At least, that’s my best attempt at an explanation.

    DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some text on an HTML page. Need a bookmarklet (no jQuery) that
I m using jquery validator plugin. i need to validate some field with some
I'm using this jQuery Validation script to submit my form(s), but I need some
I need some help regarding making a form using PHP, MySQL, and jQuery. Here
I need some help removing spaces after hiding certain HTML elements. In context, I
After inserting new html to DOM,I need to add some listeners to it. But
Edit: changing .ready() call per Matt's suggestion. I use jQuery to configure some stuff
I have a div <div id=maindiv></div> and some checkboxes to add some html element
My problem is related to using the JQuery Tools Tooltip Plugin ( http://flowplayer.org/tools/tooltip.html )
OK I have some HTML like this: <form> <h3> <table> <h3> <table> <h3> <table>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.