Solved

jQuery - access and remove <br> tag

Posted on 2014-10-12
5
1,019 Views
Last Modified: 2014-10-12
Hi E's,
I have this code, with some <br> tags:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Access and remove elements</title>
<script src="../../ficheiros_apoio/jquery.min.js"></script> 
</head>
<body>
<a class="access_remove">click here to access and remove <br></a>
<p>this is a phrase with some<br><br>we continue with more breaks<br>hello again</p>
<div>this is a line with breaks<br></div>
<a>this line break is for remove<br><br></a>
<p>thank you<br>for all</p>
<script>
$(".access_remove").click(function(){
     //?????????????????????????????
     });
</script>
</body>
</html>

Open in new window

I need to access/remove the <br>'s (<br><br>) in this line
<a>this line break is for remove<br><br></a>
How I do that?

The best regards, JC
0
Comment
Question by:Pedro Chagas
  • 2
  • 2
5 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
You have nothing to identify the element with and there is no point giving code based on a structure that is probably not the actual way it will look
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 310 total points
Comment Utility
test page : http://jsfiddle.net/7t9x3bum/
$(".access_remove").click(function(){
    $(this).nextAll("a:first").find("br").remove();
});

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
Comment Utility
Hi @leakim971, you solution just remove one <br>, and I need to remove all <br>'s of the line.

@gary
, suppose in my code I identify the elements, how I remove the br's?
<a class="access_remove">click here to access and remove <br></a>
<p class="first_p">this is a phrase with some<br><br>we continue with more breaks<br>hello again</p>
<div id="new_id">this is a line with breaks<br></div>
<a class="first_a">this line break is for remove<br><br></a>
<p class="second_p">thank you<br>for all</p>

Open in new window

0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 190 total points
Comment Utility
Then it's easy
http://jsfiddle.net/upjo6jmg/2/
$(".access_remove").click(function(){
$(".first_a").html($(".first_a").html().replace("<br><br>",""))
})

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
Comment Utility
My apologizes, @leakim971 code remove all br's in the line.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now