2

I have looked but have not found a specific answer for changing just one attribute. Here is an example I cooked: I only want to change the display attribute.

<html>
 <head>
 <title>
 test
 </title>
 <style type="text/css">
 *{
 margin: 0px;
 padding: 0px;
 }
 a.move {
 height: 25px;
 width: 25px;
 display: inline-block;
 border: 1px #aaa solid;
 border-radius: 5px;
 text-align: center;
 text-decoration: none;
 color: black;
 }
 a.move:hover {
 background-color: #aaa;
 }

---v This is the important css part

 li {
 display: inline-block;
 width: 370px;
 border: 1px #aaa solid;
 }

---v This is the important css part

 </style>
 <script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
 <script type="text/javascript">
 var current = 1;
 var wait = 0;
 function next(){
 current++;
 update();
 }
 function prev(){
 current--;
 update();
 }

---v This is the important jquery part

 function update(){
 $('p').text(current);
 $('li').each(function(index) {
 if(index != current)
 $(this).css('display','none');
 else
 $(this).css('display','inline');
 });
 }

end of the important part ---^

 $(function(){
 update();
 $('a.move.left').click(function(e){
 if(wait) return;
 e.preventDefault();
 prev();
 });
 $('a.move.right').click(function(e){
 if(wait) return;
 e.preventDefault();
 next();
 });
 });
 </script>
 </head>
 <body>
 <p> ... </p>
 <a class="move left" href="a">&lt;</a>&emsp;<a class="move right" href="b">&gt;</a></br>
 <ul>
 <li>123</li><!--
 --><li>123</li><!--
 --><li>456</li><!--
 --><li>789</li><!--
 --><li>abc</li><!--
 --><li>edf</li><!--
 --><li>ghj</li><!--
 --><li>!@#</li><!--
 --><li>$%^</li><!--
 --><li>ABC</li>
 </ul>
 </body>
</html>

Please advise. Currently, what happens is that it removes all the other css attributes. I could set all the attributes every time but I do not wish to.

Thanks

asked Jun 19, 2012 at 8:47
2
  • Don't quite understand which li do you want to change but you are using the each function which changes each of every li element. Commented Jun 19, 2012 at 8:52
  • Could you explain in more detail the "removes all the other CSS attributes" part of the question? It seems to be OK in this demo Commented Jun 19, 2012 at 8:54

4 Answers 4

2

Modifying the css directly is (in most cases) not a good option.

instead either use:

$(this).show() and $(this).hide()

or (my personal preference, since this is even more flexible):

$(this).toggleClass("...") and create according CSS-classes in your stylesheet.

Additionaly, you are changing the css of ALL you liitems: $('li').each - is this really what you want?

The .css() method only changes the css-attribute you specify - it does not override other styles. If this seems to be the case, the problem probably lies somewhere else in your code.

answered Jun 19, 2012 at 8:53

3 Comments

Yeah, I changed to inline instead of back to inline-block and I was surprised that I was losing the width. Thanks :)
I also noticed I don't need to iterate with .each and could simple use the eq() selector
@ArthurWulfWhite you are welcome. I was wondering if it is just the inline declaration with losing the width but then i forgot to write this in my answer;)
1

Use

$(this).toggle()

or If you want to show or hide use show and hide as you required.

$(this).show() 
$(this).hide() 
answered Jun 19, 2012 at 9:06

Comments

0

Instead of that, why now use:

$(this).show() or $(this).hide() as needed... ot $(this).toggle() ....

answered Jun 19, 2012 at 8:51

Comments

0

In jquery you can hide or show elements using:

$(this).show() or $(this).hide() instead of changing the css.

answered Jun 19, 2012 at 8:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.