LewlSauce
LewlSauce

Reputation: 5862

How to change select option with jquery?

So I have a table with the class "vuln-table" and the first column is full of checkboxes, while the last column are options

I've basically figured out how to do an alert box for only rows that contain a checked checkbox; however, I now need to figure out how to change the value of the selected option

Here's my js file:

function change_severity(severity) {
    $(".vuln-table tr").each(function() {
        $(this).find('td input:checked').each(function() {
            // this row has a checkbox selected
            // this row needs to have the select value changed
        });
    });
}

Here's the HTML that contains my options

<tr role="row" class="even">
<td class="sorting_1"><input type="checkbox" name="asfasdfd" id="vuln_checked_" value="asdfsdfa"></td>
<td>something</td>
<td><select class="form-control" name="test" id="kjhtkjehtehr"><option value="Severe">Severe</option>
<option value="High">High</option>
<option value="Medium">Medium</option>
<option selected="selected" value="Low">Low</option>
<option value="Informational">Informational</option>
</select>
</td>
<td></td>
</tr>

I'm passing in severity through a button click, and it always equals to one of those values. I just basically want to have the selected option to change to whatever "severity" is set to. So if I pass "Severe" to the function, then the selected option should now be "severe".

Upvotes: 0

Views: 398

Answers (4)

Jobelle
Jobelle

Reputation: 2834

function change_severity(severity) {
$(".vuln-table tr").find('td input:checked').each(function() {
   $(this).closest('tr').find("select").val(severity);
});

}

change_severity("Informational")

Upvotes: 0

Thanga
Thanga

Reputation: 8091

http://jsfiddle.net/v1pje44v/12/

The below function fix the issue

function change_severity(severity) {
$(".vuln-table tr").each(function() {
    $(this).find('td input:checked').each(function() {
        $(this).parent().parent().find("select").val(severity);
    });
});
}

Upvotes: 2

Michał Perłakowski
Michał Perłakowski

Reputation: 92461

Try this:

function change_severity(severity) {
    $(".vuln-table tr").each(function() {
        $(this).find('td input:checked').each(function() {
            $(this).parent().parent().find('option[value="option value"]').remove();
            $(this).parent().parent().find("option:selected").val("option value").text("option value");
        });
    });
}

Upvotes: 1

sundar
sundar

Reputation: 408

function change_severity(severity) {
    var tr;
    $(".vuln-table tr").each(function() {
        tr = $(this);
        $(this).find('td input:checked').each(function() {
            tr.find('td select').val('medium'); // add any option you want to change
        });
    });
}

Upvotes: -1

Related Questions