user3386779
user3386779

Reputation: 7175

change the default text in selectbox using jquery

I have select box with the id 'edit-field-service-line-tid' which is having the default text '-Any-' for default value.

This select field is generated by drupal. So I want to change the text '-Any-' to 'Success Stories' in jquery.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="edit-field-service-line-tid" name="field_service_line_tid" class="form-select">
  <option value="All" selected="selected">- Any -</option>
  <option value="1">Retail</option>
  <option value="2">Big Data</option>
  <option value="3">Cloud</option>
</select>

Upvotes: 0

Views: 2436

Answers (5)

Aman Kumar
Aman Kumar

Reputation: 4547

$(function() {
$('#edit-field-service-line-tid option:eq(0)').text('Success Stories');
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<select id="edit-field-service-line-tid" name="field_service_line_tid" class="form-select">
<option value="All" selected="selected">--All--</option>
<option value="1">Retail</option><option value="2">Big Data</option>
<option value="3">Cloud</option>
</select>

Upvotes: 1

sujivasagam
sujivasagam

Reputation: 1769

just change the value of the select box as you do for the textbox like

$("#selectboxid).val('selectvalue');

Upvotes: 0

Noman
Noman

Reputation: 4116

Simply get the first element of option by id and change its text.

BY ID:

$($("#edit-field-service-line-tid option")[0]).text('Success Stories')

Upvotes: 1

rexroxm
rexroxm

Reputation: 878

Try this

$(function(){
$('select option:contains("- Any -")').text('Success Stories');
});

Upvotes: 0

Rahul
Rahul

Reputation: 18557

Try this,

$('#edit-field-service-line-tid option[value="All"]').text('Success Stories');

It should work.

Upvotes: 4

Related Questions