0

I have these html tags:

 <select name="ct" id="ct">
        <option value="-1">Tutte</option>
        <option value="1">Da verificare</option>
        <option value="2">Verificate</option>
        <option value="3">Approvate</option>
        <option value="4">Respinte</option>
        <option value="5">Pubblicate</option>
        <option value="6">Scadute</option>
        <option value="7">Proposte</option>
        <option value="8">Rifiutate</option>
        <option value="9">Ritirate</option>
        <option selected="selected" value="7,8,1">Proposte / Rifiutate / Da verificare</option>
   </select>

I want to change the attribute value of the selected option (which contains "7,8,1") to value="-1", so it will look like:

<option selected="selected" value="-1">Proposte / Rifiutate / Da verificare</option>

I tried with:

$dom_richieste->getElementsByTagName('options')->getAttribute('value').value="-1";

But that's not working...

1
  • $dom_richieste->getElementsByTagName('options')->getAttribute('value').value="-1"; makes no sense and is invalid in both Javascript and PHP. Show how you set $dom_richieste. Commented Aug 27, 2018 at 13:24

3 Answers 3

3

You can use $("#ct option[value='7,8,1']").val("-1");

$("#ct option[value='7,8,1']").val("-1");
console.log($("#ct").val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="ct" id="ct">
        <option value="-1">Tutte</option>
        <option value="1">Da verificare</option>
        <option value="2">Verificate</option>
        <option value="3">Approvate</option>
        <option value="4">Respinte</option>
        <option value="5">Pubblicate</option>
        <option value="6">Scadute</option>
        <option value="7">Proposte</option>
        <option value="8">Rifiutate</option>
        <option value="9">Ritirate</option>
        <option selected="selected" value="7,8,1">Proposte / Rifiutate / Da verificare</option>
   </select>

I suggest you to check how you created this drop down, and if possible replace there instead after creating drop down.

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

1 Comment

This is very specific to the question - make it more generic by searching the 'selected' attribute
1

If I understand correctly you are need something like:

foreach ($dom->getElementsByTagName('option') as $item) {
    if ($item->getAttribute('selected') == "selected")
        $item->setAttribute("value", "-1");
}

This way you pass on your option items and set the value of the selected ones

Comments

0

You have used getElementsByTagName('options') in your code and no tag available with name options. Instead you should use correct tag name option.

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.