0

I am working on a question and answer comparison example where i am trying to compare question string with json data. Questions and answers will be pulled as json data. Could you please help me guiding fix for comparison problem.

My code is here

<label for="What_is_planning">What is planning</label>
<br>
<label for="What_is_implementing">What is implementing</label>
<br>
<div class="result"></div>


$(document).ready(function () {
    $('label').mouseover(function () {
        var helpString = this.firstChild.nodeValue.replace(':', '').trim();

        var answerString = "<br>No Answer";
        $(".result").html(" <hr><b>Guest: </b>" + helpString);
        $(".result").append(" <div><b>AskGFS: </b>" + answerString);
        console.log(helpString);
    });

    var QandAData = '{"gfsdata":[{"question":"what is planning","answer":"This is a important part of life"},' +
        '{"question":"what is implementing","answer":"this is followed by planning"}]}';

    //Comparing answers
    for (var i = 0; i < QandAData.length; i++) {
        if (QandAData[i].gfsdata.question == 'what is planning') {
            answerString = QandAData[i].gfsdata.answer;
        }
    }
});
Bas van Dijk
  • 9,933
  • 10
  • 55
  • 91
Kurkula
  • 6,386
  • 27
  • 127
  • 202

1 Answers1

1

Try:

$(document).ready(function () {
    $('label').mouseover(function () {
        var helpString = this.firstChild.nodeValue.replace(':', '').trim();
        var answerString = "<br>No Answer";
        var QandAData = {
            "gfsdata": [{
                "question": "What is planning",
                "answer": "This is a important part of life"
            }, {
                "question": "What is implementing",
                "answer": "This is followed by planning"
            }]
        };
        for (var i = 0; i < QandAData.gfsdata.length; i++) {
            if (QandAData.gfsdata[i].question == helpString) {
                answerString = QandAData.gfsdata[i].answer;
            }
        }
        $(".result").html(" <hr><b>Guest: </b>" + helpString+" <div><b>AskGFS: </b>" + answerString);
    });
});

Updated fiddle here.

codingrose
  • 15,563
  • 11
  • 39
  • 58