0

I am using a drop-down using JSTL (on the JSP page), this drop-down contains multiple records. Code SS

Original drop down

EXPECTED RESULT: There should be new "Value" on drop down and that value is not part of drop down.

<select name="salution" class="form-select" id="salution" required>
  <c:forEach items="${salution}" var="sol">
    <option value="${sol.lovVal}">${sol.lovName}</option>
  </c:forEach>
</select>

I want to show a single value on that drop-down which already holds too many values. I am using Ajax to override a single value on the same drop-down which already holds multiple values.

 var someValue="some value";                  
                $('#salution')
                        .find('option')
                        .remove()
                        .end()
                        .append('<option value="${sol.lovVal}" >${someValue}</option>');

also tried this one, I have to just override ${someValue}.

var someValue="some value";                  
                $('#salution')
                        .find('option')
                        .remove()
                        .end()
                        .append('<option value="1" >${someValue}</option>');

Any solution?

  • You want to remove a *single* ` – freedomn-m Sep 24 '22 at 14:01
  • I want to remove all values from the drop-down and show a new value. (And want to make it as read-only field) I have seen this .find('option') on some blog. I don't have any idea about that why we even use that. – Sarmad Soz Sep 24 '22 at 16:17
  • I have used .find("option:selected") one, but it took value based on ID which are already part of dropdown. In my case, I have to set a value which is completely new. – Sarmad Soz Sep 24 '22 at 16:30
  • If will be easy to answer if you'll provide initial HTML of `` – Vijay Hardaha Sep 24 '22 at 17:10
  • Please, Review the QUESTION now. – Sarmad Soz Sep 24 '22 at 17:48
  • `.find("option").remove()` removes all the options. What is your code doing that you don't want it do / not doing that you do want it to do? – freedomn-m Sep 24 '22 at 18:03
  • Off Topic: always use a free-form box for salutation unless you're going to code off it (eg "if user is Dr then do...") which is very unlikely. There will *always* be another valid salutation that you've haven't included and it's very frustrating for the user to not be able to put in their real, earned salutation. – freedomn-m Sep 24 '22 at 18:05
  • `.append('');` or use a backtick `.append(\`\`);` – freedomn-m Sep 24 '22 at 18:06
  • See this question: https://stackoverflow.com/questions/1408289/how-can-i-do-string-interpolation-in-javascript – freedomn-m Sep 24 '22 at 18:07
  • Freedomn-m: Oooh , you saved my life. Thanks for this. `.append('');` This line is working fine – Sarmad Soz Sep 25 '22 at 06:59

1 Answers1

0

If you want to append a single value on drop-down, which already holds multiple values.

Freedom-m helped in comment: https://stackoverflow.com/users/2181514/freedomn-m

You have to use this line of code.

.append('<option value="1" >' + someValue + '</option>');