# Connecting an HTML tag to a CSS class with Javascript

**URL:** <https://forum.kirupa.com/t/connecting-an-html-tag-to-a-css-class-with-javascript/310242>\
**Category:** web dev\
**Created:** [June 5, 2010, 7:30am UTC](https://forum.kirupa.com/t/connecting-an-html-tag-to-a-css-class-with-javascript/310242 "2010-06-05T07:30:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dev89](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@dev89](https://forum.kirupa.com/u/dev89)\
**Post date:** [June 5, 2010, 7:30am UTC](https://forum.kirupa.com/t/connecting-an-html-tag-to-a-css-class-with-javascript/310242/1 "2010-06-05T07:30:51Z")

</div>

Hello,

I’m a newbie in web development, and I really need help on how to connect an HTML tag to a CSS class with Javascript. The content of the HTML tag itself will be inserted to the page through an XML file (which in turn, is being inserted to the page with Javascript).

It’s for the lyrics section of a website, and I’m looking to do something like this for it:  
[http://roshanbh.com.np/examples/exapandable-panel/](http://roshanbh.com.np/examples/exapandable-panel/)  
And this is the tutorial I followed for the Jquery collapsible panel:  
[http://roshanbh.com.np/2008/03/expandable-collapsible-toggle-pane-jquery.html](http://roshanbh.com.np/2008/03/expandable-collapsible-toggle-pane-jquery.html)

and I’m planning to have the whole thing to be inserted to the page with pure XML, so that it would be easy for the client to update the website in the future (new tab will appear everytime the client update the XML file, etc.).

The Jquery collapsible panel seems to rely heavily on the CSS classes.

This is what I have so far:  
\<div class=“msg\_list”\>

_ **CSS** _

\<style type=“text/css”\>  
body {  
margin: 10px auto;  
width: 570px;  
font: 75%/120% Verdana,Arial, Helvetica, sans-serif;  
}  
p {  
padding: 0 0 1em;  
}  
.msg\_list {  
margin: 0px;  
padding: 0px;  
width: 383px;  
}  
.msg\_head {  
padding: 5px 10px;  
cursor: pointer;  
position: relative;  
background-color:#FFCCCC;  
margin:1px;  
}  
.msg\_body {  
padding: 5px 10px 15px;  
background-color:#F4F4F8;  
}  
\</style\>

\*\*\* end CSS \*\*\*

_ **inserting the title with XML** _

```
&lt;script type="text/javascript"&gt;
    if (window.XMLHttpRequest) {
      xhttp=new XMLHttpRequest();
  } else {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET","xml/musicpage_lyrics.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;

var x=xmlDoc.getElementsByTagName("songs");
for (i=0;i&lt;x.length;i++) {
    document.write(x*.getElementsByTagName("title")[0].childNodes[0].nodeValue);
    document.write("&lt;br&gt;");
  }
&lt;/script&gt;

```

_ **end of inserting the title with XML** _

_ **inserting lyric with XML** _

```
&lt;script type="text/javascript"&gt;

    if (window.XMLHttpRequest) {
      xhttp=new XMLHttpRequest();
  } else {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET","xml/musicpage_lyrics.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;

var x=xmlDoc.getElementsByTagName("songs");
for (i=0;i&lt;x.length;i++) {
    document.write("&lt;p&gt;");
      document.write(x*.getElementsByTagName("lyric")[0].childNodes[0].nodeValue);
    document.write("&lt;/p&gt;");
  }
&lt;/script&gt;

```

_ **end of inserting lyric with XML** _

_ **animate the Jquery collapsible panel** _

```
&lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;

```

\<script type=“text/javascript”\>  
$(document).ready(function(){  
//hide the all of the element with class msg\_body  
$(".msg\_body").hide();  
//toggle the componenet with class msg\_body  
$(".msg\_head").click(function(){  
$(this).next(".msg\_body").slideToggle(600);  
});  
});  
\</script\>

_ **end of animate Jquery collapsible panel** _

I tried adding the class by doing something like this in the JS code:  
document.write("\<p class=“msg\_body”\>");

but it doesn’t work.  
Please let me know if anyone can help, or direct me to a better solution.  
Thanks before! 🙂
