1

i have a simple javascript that add style to a slider when is turn to show, but i want to remove the style when the next slider come and also on mouse over remove the both style.

This is my javascript function that add style="width: 200px" to new slider and add style="width: 200px" to old.

function test(){
    var read = document.getElementById("slide-0") // how can i add here to read the incremented slide-0, slide-1, slide-2
        .removeAttribute("style",0);
}

    function noEffect(pOld,pNew){

        if (pOld){
            pOld.style.width='10px';
        }

        if (pNew){
            pNew.style.width='200px'; //this style become on active slider.
        }
    }

Slider html

<div class="slider" onMouseOver="test()">
<ul>
<li id="slide-0" class="slide" style="width: 10px"><img src="image.jpg"></li>
<li id="slide-1" class="slide" style="width: 200px"><img src="image.jpg"></li> <!-- this is active now -->
</ul>
</div>

Exactly what i want is to remove the style on both on mouseover.

Any help is appreciated.

3
  • the id "slide-0" is repeated, ID can not be repeated. <li id="slide-0" class="slide" style="width: 10px"><img src="image.jpg"></li> <li id="slide-0" class="slide" style="width: 200px"><img src="image.jpg"></li> Commented Jun 29, 2013 at 12:35
  • yes, sorry i copy paste, i modified now. Commented Jun 29, 2013 at 12:49
  • Nice. Glad you managed to solve your problem. Commented Jun 29, 2013 at 16:35

2 Answers 2

3

specify 0px

pOld.style.width='0px';
Sign up to request clarification or add additional context in comments.

4 Comments

yes, on apply pOld.style.width='0px'; is hiding the slider block.
and also on mouse over other slider li, one of slider li have width: 200px and i need to remove on mouse over.
Hi @PSR, lets say that i figure it out, but how can i read the incremented slide0, slide-1... see my updated question
2

A very nice solution would be to uses classes. Instead of adding a style, add and remove classes. The following code adds a class to the component that will be styled.

var d = document.getElementById("div1");
d.className = d.className + " widthAlteration";

And in your CSS have:

.widthAlteration
{
    //width alteration
}

And if you would want to revert the effect, just do the same:

var d = document.getElementById("div1");
d.className = "";

Or for convenience there is integrated command for that:

var ele = document.getElementById("div1");
addClass(ele, "widthAlteration");
// or
removeClass(ele, "widthALteration");

Seeing as the previous part was not working, I will suggest jQuery

Implement the .js file in your project and use jQuery. It is one of the most commonly used libraries. It is rare to see JS done in raw (maybe if you only need to perform a simple operation), but in general cases, jQuery is the way to go.

After implementing, add this to your project:

<script>
$( document ).ready(function() {
//$("here goes the ID, class, component w.e. you desire")
    $(".widthAlteration").click(function(){
        $(this).removeClass(".widthAlteration");
    });
});
</script>

8 Comments

@Alexey - Where are addClass() and removeClass() defined? (You said they're "integrated", but they're not standard JS/DOM functions.) Also, note that d.className = ""; will remove all classes from the element in question.
I was going to suggest jQuery, but thought that would be off topic. I thought these function were defined in raw JS.
I have an issue with Jquery, my website don't want to read some Jquery functions, thats because i want to extend my actual javascript, please have a look at my updated question. Thanks a lot
i understand you, the issue is that to change it to jquery i need to change the hole script. Thanks a lot
Change the whole script? It is just a library. You can just add my function and use the rest of your JS as it is. It doesn't force you to only use jQuery.
|

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.