0

I have a function that should detect which item is selected in dropdown list then change the form, this could be hiding form fields or assigning a value to a field. The hiding/showing of fields is working as it should however the value of the field does not update and is staying blank.

if (document.getElementById('service16').value == '4989') {
  $('#valuediv16').show();
  $('#namediv16').hide();
  $('#qtydiv16').hide();
  $('#cbdiv16').hide();    
} else if (document.getElementById('service16').value == '4946') {
  $('#cbdiv16').hide();
  $('#valuediv16').show();
  $('#value').prop("disabled", false);
  $('#namediv16').hide();
  $('#qtydiv16').show();
  $('#esetdiv16').hide();
  document.getElementById('value').value = "11.70";
} else if (document.getElementById('service16').value == '4987') {
  $('#cbdiv16').hide();
  $('#valuediv16').show();
  $('#value').prop("disabled", false);
  $('#namediv16').hide();
  $('#qtydiv16').show();
  $('#esetdiv16').hide();
  document.getElementById('value').value = "3.80";
} else if (document.getElementById('service16').value == '4988') {
  $('#cbdiv16').hide();
  $('#valuediv16').show();
  $('#value').prop("disabled", false);
  $('#namediv16').hide();
  $('#qtydiv16').show();
  $('#esetdiv16').hide();
  document.getElementById('value').value = "9.40";
}
2
  • document.getElementById('value').val("9.40") Commented Sep 7, 2017 at 12:46
  • 1
    Just FYI, it'd be worth researching the DRY principle to improve your code Commented Sep 7, 2017 at 12:50

2 Answers 2

2

first of all, if you are using JQuery, you should always use it and not switch between plain javascript and JQuery.

The line

document.getElementById('service16').value == '4988'

can be written in JQuery like

$("#service16").val() == '4988'

That said, converting

document.getElementById('value').value = "9.40";

to

$("#value").val('9.40')

Are you sure you are using 'value' as an id? Have you tried using another id? It's a string, but it doesn't look like a great practise to use value as an id name.

Sign up to request clarification or add additional context in comments.

2 Comments

Jquery is Javascript. To say you shouldn't switch is silly.
ProEvilz, yes of course, but imo when you choose JQuery, stick with it, even if it's only for readability / colleague consensus
0
 $('#service16 option:selected').html();  

or

 $('#service16 option:selected').val();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.