1

I have following code in cshtml page:

<ul id="menu">
  <li><a href="..." >Link1</a></li>
  <li><a href="..." >Link2</a></li>
</ul>

And I want to change class of a tag when some condition is true. I tried this, but it does not work:

<ul id="menu">
  <li 
    <script> if (..condition..) 
      document.write(" class = \"newclass\"");
    </script>
  ><a href="..." >Link1</a></li>
  <li><a href="..." >Link2</a></li>
</ul>

Could you please suggest the way to do it?

Thanks, Zhenya

3
  • 1
    this is not a valid HTML syntax . Commented Jun 18, 2013 at 10:30
  • 1
    What is the condition that needs to be met? Should the class apply to all li elements under menu or a specific li element? If a specific one, which element? Commented Jun 18, 2013 at 10:32
  • document.write writes to the output... and not to the markup itself! Commented Jun 18, 2013 at 10:36

3 Answers 3

2

Try giving the li an ID as shown below:

 <li id="list"><a href="..." >Link1</a></li>
 <li><a href="..." >Link2</a></li>

Then in your JavaScript try:

if (condition) {
   var element = document.getElementById("list");
   element.className = "newclass";
}

JSFiddle:

http://jsfiddle.net/bVGFP/

Sign up to request clarification or add additional context in comments.

1 Comment

I just edited the variable reference when setting the className property.
0

Please give an id to the li

<li id="test"></li> access the <li> by id

$("#test").addClass("classname");

1 Comment

What is $ here? It's not a native DOM or JavaScript function.
0

You have to use document.innerHTML because document.write need the reload page.

<ul id="menu">
                <li  
                    //This is not valid syntax
                    <script> if (..condition..) 
                                document.write(" class = \"newclass\"");
                    </script>
                   ><a href="..." >Link1</a></li>
                <li><a href="..." >Link2</a></li>
            </ul>

Then for you case, I suggest for you

    <ul id="menu">
                    <li id="po"><a href="..." >Link1</a></li>
                    <li ><a href="..." >Link2</a></li>
                </ul>

<script>
document.getElementById("po").className = "newclass";
                        </script>

2 Comments

"document.write need the reload page" That's correct. Once the document was completely parsed, documen.write will overwrite the whole document, it won't reload the page. However using document.write while the document is still being parsed is perfectly fine (theoretically). It will just insert the data where it is called. The problem here is that the OP tried to use <script> tags inside an opening tag of another element which is simply invalid HTML. Using document.write is not the problem, although using it is not very good either.
Thx DArren for the suggst and @FelixKling for the best suggest. And I not know if I have to remove my answer because this will help to someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.