So I have a variable with cValue
what stands for "current value". The problem however whenever my input fields turn into buttons is: Multiple change to buttons and therefore it does not hold a value any longer. I try to assign the values back to the buttons, but since I am refering to cValue (the current value) it will change all the input fields to the last filled out value given. How can I keep the value I had inserted to the input field?
example for clarification: you have 3 input fields, when all 3 are filled out correctly (it matches it vs a JSON file), all the 3 input fields will turn into buttons to indicate you completed that part. However whenever I try to assign the value you had given to the 3 input fields (first input got '1' as value, second as '2' and the third as '3') it will change the value of all the buttons to the last filled out input field. So that would be 3. How can I keep it so that the buttons will show: the first button '1', second '2' and the third '3'?
How my code looks like:
$.map(exercise.syllables, function (syllable, j) {
if (!syllable || !syllable.trim().length) {
// If it doesn't exist or is an empty string, return early without creating/appending elements
return;
}
var innerSylCol = $('<div/>', {
class: 'col-md-3 inputSyllables'
});
var sylInput = $('<input/>', {
'type': 'text',
'class': 'form-control syl-input',
'name': +c++,
'id': +idsyll++
}).on('blur', function() {
var cValue = $(this).val();
if(cValue === "") {
return;
}
if (cValue === syllable) {
correctSylls.push(cValue);
console.log(correctSylls);
}
if (exercise.syllables.length === correctSylls.length) {
$(this).closest('.syll-row').find('input.syl-input').replaceWith(getCorrectBtn(cValue));
// console.log(getCorrectBtn(cValue));
S.addRight();
S.playRight();
} else if (cValue !== syllable){
$(this).css({'color':'#e00413'});
S.playWrong();
S.addWrong();
}
});
The part that most likely is wrong:
$(this).closest('.syll-row').find('input.syl-input').replaceWith(getCorrectBtn(cValue));
the function where I create the button:
function getCorrectBtn(value) {
var correctBtn = $('<button/>', {
'class': 'btn btn-success buttonCorrect',
'type': 'button',
'id': "button" + CBC++,
'value': value
});
}
EDIT: The answer that has been checked is CORRECT. However this is the end result, when I inspect them they do have the desired value... But it doesn's show it in my front-end: