0

I have the two divs, One for questions another one for answers. if the div id ques0 has here class <div id="ques0" class="showquestion here"></div>, respect to answers id qstats0 of parent tr added class of highlight.

HTML

<!--Question-->
    <div id="ques0" class="showquestion here"></div>
    <div id="ques1" class="hidequestion"></div>
    <div id="ques2" class="hidequestion">

<!--Answer-->
    <tr>
    <td valign="top">1</td>
    <td valign="top" id="qstatschoice0">A</td>
    <td id="qstats0">N</td>
    </tr>

    <tr>
    <td valign="top">2</td>
    <td valign="top" id="qstatschoice1">A</td>
    <td id="qstats1">N</td>
    </tr>

    <tr>
    <td valign="top">3</td>
    <td valign="top" id="qstatschoice2">A</td>
    <td id="qstats2">N</td>
    </tr>

I want output like below.

<!--answer-->
    <tr class="highlight">
    <td valign="top">1</td>
    <td valign="top" id="qstatschoice0">A</td>
    <td id="qstats0">N</td>
    </tr>
Azzah
  • 71
  • 1
  • 9
  • 1
    Hint: what your mainqus and popqus variables contain are strings - not the ids. (And they will never equal each other, because one starts with "ques", the other with "qstat") – doldt Mar 18 '15 at 08:58
  • that's not jquery :) – iandayman Mar 18 '15 at 08:59
  • I'm guessing he means the number at the end of the id. You have to parse that. – zozo Mar 18 '15 at 08:59
  • The question value may have more values. I have to check the condition. if the particular question have '.here' class, I should find the same ques value from answers and highlight it. – Azzah Mar 18 '15 at 09:34

3 Answers3

1

The ids are not the same(and it should not be), but there is a relationship ie the numeric part of the ids are the same so you can use replace() to find out the td with id qstats<x>(where x is the numeric part of the id of the here element) then find the tr ancestor of it and highlight it.

function highlight() {
    var $c = $('.showquestion'),
        //get the id of here element
        id = $c.attr('id');
    //find the tr to highlight
    $('#' + id.replace('ques', 'qstats')).closest('tr').addClass('highlight')
}

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

I don't know where you use jQuery..

document.getElementById('current_question')
$("#current_question") // jquery way ;-)

try smthg

$("#an_id").addClass("blabla");
$("#an_id").removeClass("blabla");

btw, if many div's have the same ID, i think only the last one in the DOM will match.

bln
  • 310
  • 1
  • 5
  • The question value have more values. I have to check the condition. if the question have '.here' class i should find the same ques value and highlight – Azzah Mar 18 '15 at 09:39
0

First: document.getElementById is clean javascript, not jquery. Second: try something like this:

var question = $('#ques0').text();
var answer = $('#qstats0').text();
if (question == answer) {
    answer.parent('tr').addClass('highlight');
}

Some info: ids have no value. It's just identificator of an element. .text() gets all text inside selected element.


Tell me if I understood something wrong and if so - please provide more information.

Best regards.

PapaSoft
  • 53
  • 5
  • @Papsoft. The question value have more values. I have to check the condition. if the question have '.here' class i should find the same ques value and highlight. – Azzah Mar 18 '15 at 09:32