Faizan Ali
Faizan Ali

Reputation: 1013

jQuery - Change id according to the value selected from the select field

I'm trying to change the id of the select field with a JS function, please take a look at my HTML and JS code.

HTML:

<select name="cat-parent" id="change_id();"> <option selected="selected" value=""></option> <option value="category-2">Category 2</option> <option value="category-3">Category 3</option> <option value="category-4">Category 4</option> <option value="category-2">Category 2</option> <option value="category-1">Category 1</option> </select>

JS:

function change_id(){ var idSuffix = jQuery(this).val(); return 'example' + idSuffix; }

Upvotes: 0

Views: 1351

Answers (1)

Amit Joki
Amit Joki

Reputation: 59292

You need to bind change event handler.

$('[name=cat-parent]').on('change', function() {
   $(this).attr("id", 'example' + $(this).val());
});

Upvotes: 1

Related Questions