-1

In this question I would like to display some HTML text depending on a which combination of options is selected in a form. In this example for instance, I want to display some text if spelling is selected as a subcategory and 'greater-depth' (equivalent to an 'A' grade) is selected as the performance grade. I've developed this in Rails form_for but have shown the form as rendered in the browser.

<form class="new_english_grade" id="new_english_grade" action="/english_grades" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="VTtOS/86shuyQPW6/HfaduffmQiVXLiJb06IQp7+56LM8cD8KRnD3qLGbQBit4OuAIc92MYbFpPObR6ePYmY1g==" />

      <div class="field">
        <label for="english_grade_subcategory">Subcategory</label>
        <select name="english_grade[subcategory]" id="english_grade_subcategory"><option value="Spelling">Spelling</option>
    <option value="Reading">Reading</option>
    <option value="Writing">Writing</option></select>
      </div>

      <div class="field">
        <label for="english_grade_performance_grade">Performance grade</label>
        <select name="english_grade[performance_grade]" id="english_grade_performance_grade"><option value="Not-started">Not-started</option>
    <option value="Working-towards">Working-towards</option>
    <option value="Working-at">Working-at</option>
        <option value="Greater-depth">Greater-depth</option></select>
      </div>
</form>

The text I'd like to display for instance is like:

<div id = "spelling_greater_depth">
  This text is displayed only if 'spelling' and 'greater-depth' are selected in options
</div>

I have initially set my CSS to be:

#spelling_greater_depth
{
  display: none;
}

My JavaScript is not really working yet so I have not included it but I was trying to implement it using this:

Community
  • 1
  • 1
Jack Bridger
  • 165
  • 2
  • 14
  • It doesn't do anythging – Adam Buchanan Smith Aug 30 '16 at 21:48
  • If you're going to use single quotes inside a string delimited with single quotes, you have to escape them as `\'`, or switch to `"`. Look at the string in your first jQuery selector. – 4castle Aug 30 '16 at 21:52
  • Post more code. Without Apache source code, the minimal program is probably not complete enough. Are you asking how to update the content of HTML element in JavaScript? Will you ask a separate question for updating every HTML element? – Little Alien Aug 30 '16 at 21:53
  • Yes I would like to just change the HTML depending on which combination of the two form options are selected. I just want to see how it works for one example and then I should be able to do it - sorry I will update question – Jack Bridger Aug 30 '16 at 21:55

1 Answers1

1

I think this might be enough to get you started https://jsfiddle.net/sxh0n7d1/37/

However it is very difficult to answer the question, can you clarify your question or give feedback to this answer if it is close?

 $('select[name="english_grade"]').change(function () {
  $('#spelling_working_at').css("display","none");
  console.log($(this).val());
  var fieldToShow = $(this).val();
  $("#" + fieldToShow).css("display","block");
});
Adam Buchanan Smith
  • 9,422
  • 5
  • 19
  • 39
  • Thanks so much, I really appreciate your help. I have amended my question does it make it clearer? – Jack Bridger Aug 30 '16 at 22:00
  • The crux of the question is that I want to display certain text if a certain combination of the two options is selected. – Jack Bridger Aug 30 '16 at 22:02
  • 1
    @aurelius2016 oh, gotcha. yeah, I am going on lunch but if it isn't answered by the time I return I will take care of that for you. – Adam Buchanan Smith Aug 30 '16 at 22:06
  • Thanks so much dude, you're a hero :D Really appreciate it :D – Jack Bridger Aug 30 '16 at 22:09
  • thanks again, I just had one extra question related to the solution you provided. So in the current example, when you select Spelling and greater-depth, some text appears saying 'greater-depth' which is exactly what I was asking. I thought it would be obvious but I can't work out why this only applies to spelling. I'd like to edit what you've written so that I can get different text for each of the permutations for subcategory and performance grade and at the moment only spelling and a performance grade leads to this and I don't understand why? Thanks! – Jack Bridger Aug 31 '16 at 09:38
  • 1
    @aurelius2016 that would be my fault, seems to be a bit buggy. I was trying to build you an example using what you already had in your page. If I can build it completely different I can make a pretty bitchen example if you like. – Adam Buchanan Smith Aug 31 '16 at 16:43
  • Ah OK! That would be amazing! – Jack Bridger Aug 31 '16 at 16:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122346/discussion-between-aurelius2016-and-adam-buchanan-smith). – Jack Bridger Aug 31 '16 at 22:01