jeskey boneman
jeskey boneman

Reputation: 227

select the specific class who been clicked from few same classes

I trying as a beginner in JS to get the specific innerHTML that clicked from the options. they have the same class so this is the real problem. how can i "point" to specific class the been clicked in var vup?

my code:

<script>
    $(function() {
        $(".up").click(function () {
            var vup=document.getElementsByClassName("up").innerHTML;
            var qid=document.getElementsByName("id").value;
            document.getElementById("sadas").innerHTML=qid+"<br>"+vup;
        });
    });
</script>
    <input name="id" value="5" type="hidden">
        <a class="up" title="אם אתה מתחרט על הצבעה זו.">vote up</a>
        <b>0</b>
        <a class="up" title="אם שאלה זו שטוחה או פוגענית או חוזרת על עצמה או פשוטה." style="background-position:0 -220px;">vote down</a>

<div id="sadas">sda</div>

Upvotes: 1

Views: 33

Answers (2)

aahhaa
aahhaa

Reputation: 2275

use $(this) inside your click function

$('.up').click(
    function() {
    $(this).html('this will apply only to the specific .up class that is clicked, no matter how many .up class you declare');
    }
); 

than you can use $(this).closest('') or .next() or other tree traversal to get to the desired dom you want to manipulate.

Upvotes: 2

AngularLover
AngularLover

Reputation: 364

In jQuery that's

$(.up).click(function(event) {
    var text = $(event.target).text();
})

;

Upvotes: 0

Related Questions