Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ignoring Tags using Javascript

Status
Not open for further replies.

Krus1972

Programmer
Mar 18, 2004
145
US

Is there any way in Javascript that I can have the visitors' browser ignore ALL <br> tags?

Some visiotrs need to see the data with the line breaks and sometimes they do not.

Physically the <br> tags always have to be in the HTML code and CANNOT be embedded in any ASP,PHP or any server side scripting. The <br> tags must be in the HTML body of the webpage,but, I am looking for a method in Javascript that can tell the visiotrs' browser to ignore them but stil be present in the HTML.

Can anyone help?

Thanks,

Jeff

 
My Mistake. The actuall problem I am having is this:

<p>I have some text here</p>
<p>I have some more text here</P>
<p>I have even more text here></p>

If I use the following code in the style sheet then NONE of the text gets displayed:

p
{
display: none;
}


I need to be able to STILL display the text but have the <p> tags to be ignored. Removing the <p> tags from the HTML is not an option nor is using server side scripting an option.

Is there any Javascript that can accomplish this?

Thanks,

Jeff
 
Just curious, for what reason are you wanting to get rid of the P tags? You can get rid of the default browser attributes of that tag to make it seem like it's not there.

i.e.
p {
margin:0px;
padding:0px;
/* you could even use */
float:left;
}
 
That works.

One last thing. Instead of removing all <p> tags is there a way I can ignore EVERY OTHER <p> tag?


Thanks,
Jeff
 
Yes - get all P elements using DOM methods:

Code:
var pCollection = document.getElementsByTagName('p');

Then simply loop around that collection, increasing your loop count by 2 each iteration. Inside each iteration, set whatever style or classname you want on the elements to "hide" them.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
The next best thing would be to remove the P tags dynamically. Off the top of my head, it might look something like this.

function removePTags()
{
var p=document.getElementsByTagName("p");
var parent;
var i;
for (i=p.length-1; i>=0; i--)
{
parent=p.parentNode;
parent.removeChild(p);
}// end for i
}// end removePTags

My Mistake. The actuall problem I am having is this:

<p>I have some text here</p>
<p>I have some more text here</P>
<p>I have even more text here></p>

If I use the following code in the style sheet then NONE of the text gets displayed:

p
{
display: none;
}


I need to be able to STILL display the text but have the <p> tags to be ignored. Removing the <p> tags from the HTML is not an option nor is using server side scripting an option.

Is there any Javascript that can accomplish this?

Thanks,

Jeff

 
I forgot to add....
Try testing that code by removing your bit of CSS first and see if the text within the P tags remain. If they still disappear, then you have to add more to the loop, such as:
- Obtain text in the P tag's childnode.
- remove P
- Add text to former P tag's parent node.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top