circariem
circariem

Reputation: 11

onclick event with ul li to fire a function

I'm new to coding and I need help with adding an onclick event to my list (which is supposed to work as a drop-down menu)-

This is the code for my list:

<div class="click-nav">

<ul class="clicker">
<ul>
            <li><a href="#">shake</a></li>
            <li><a href="#">shrink</a></li>
            <li><a href="#">melt</a></li>
        <li><a href="#">disperse</a></li>
</ul>     
</ul>
</div>

I want to make it so that when one of these items is selected, a function is fired (which calls a bunch of images specific to the category selected to be placed in a div).

I've tried it also with the select option and it was working perfectly, but I have to change it because its stubborn design:

<label>
<select name="alphabets" id="typeface" onchange="myType();">
<option value="shake" selected>shake</option>
<option value="shrink">shrink</option>
<option value="melt">melt</option>
<option value="disperse">disperse</option>
<option value="disintegrate">disintegrate</option>

</select>
</label>

Thanks in advance for the help!

Upvotes: 1

Views: 27884

Answers (2)

black_pottery_beauty
black_pottery_beauty

Reputation: 879

See this.

<html>
<div id="name">text</div>

<ul><li onclick="chaangeEvenet(this)">A </li><li  onclick="chaangeEvenet(this)">B </li><li  onclick="chaangeEvenet(this)">C </li></ul>
</html>

<script>
var namefield=document.getElementById("name");
function chaangeEvenet(e){
   namefield.innerHTML=e.innerHTML;
}
</script>

Upvotes: 0

A Mind United
A Mind United

Reputation: 340

You could just add onclick to each '' and call a different function for each one:

<script>
    function shake(){
        alert('Shake');
    }

    function shrink(){
        alert('Shrink');
    }

</script>
<div class="click-nav">

<ul class="clicker">
<ul>
    <li><a href="#" onclick=shake();>shake</a></li>
    <li><a href="#"onclick=shrink();>shrink</a></li>
    <li><a href="#">melt</a></li>
    <li><a href="#">disperse</a></li>
</ul>     
</ul>
</div>

Upvotes: 1

Related Questions