Thread:Dessamator/@comment-5065259-20160222210830/@comment-24473195-20160225222455

Try this :

$(function {    'use strict';    var namespaceId = mw.config.get("wgPageName");

if ($("#my-dropdown").length && namespaceId!== 10) { var page =mw.config.get("wgPageName"); var defaultVar = encodeURI($("#my-dropdown").text); $("#my-dropdown").html(" pie cookie  cake  Save ");

$('#idInfoboxDropdown option').each(function {           if ($(this).val == defaultVar)             {                 $(this).attr("selected","selected");                return;            }         }); $('.dropdown-save-button').click(function {                   $.get(mw.util.wikiScript( 'api' ), { 'action': 'query', prop: 'info|revisions', intoken: 'edit', titles: page, rvprop: 'content', rvlimit: '1', indexpageids: 'true', format : 'json' }, function( response ) { var option = $('#idInfoboxDropdown').val; var pages = response.query.pages[response.query.pageids[0]]; var articleContent = pages.revisions[0]['*']; var templateName = encodeURI($( "#my-dropdown" ).data( "templatename")); var parameterName = encodeURI($( "#my-dropdown" ).data( "parametername")); var templatePrefixIndex = articleContent.indexOf("');               var templateContent = articleContent.substring(templatePrefixIndex, templateSuffixIndex + 2);                var valueIndex = templateContent.match(/\|\s*value/);                var newtemplateContent;

if (valueIndex && valueIndex.index >-1){ var remainingContent = templateContent.substring(valueIndex.index+1); var delimiterIndex = remainingContent.indexOf("|");

if (delimiterIndex == -1) { delimiterIndex = templateSuffixIndex; }                   var oldKeyVal = remainingContent.substring(valueIndex,delimiterIndex); newtemplateContent = templateContent.replace(oldKeyVal, "value = "+ option +"\n"); }               else { var searchMask = "{{" + templateName; var regEx = new RegExp(searchMask, "i"); newtemplateContent = templateContent.replace(regEx, "{{" + templateName + "\n|"+ parameterName +" = "+ option); }

articleContent = articleContent.replace(templateContent, newtemplateContent); $.post(mw.util.wikiScript( 'api' ), {                   minor : true,                    format : 'json',                    summary : "Changed parameter using dropdown to :" + option,                    action : 'edit',                    title : page,                    basetimestamp : pages.revisions[0].timestamp,                    startimestamp : pages.starttimestamp,                    token : pages.edittoken,                    text : articleContent                }, function( data ) {                    if (!data.error) {                        console.log('Succesfull !');                    } else {                        console.log('Failed.');                        $('#text-error-output').append('Failed to edit page '+page+': '+data.error.info+' ');                    }                }); });       });    } });

Template content: 