4

I have a dynamic input which will have checkbox to hide the inputs when you tick the checkbox, at the moment I'm trying to add click="getChk() to the checkbox however it was only giving me the last index inputName.

Say I have input Ids (code, sku, id).

My dynamic inputs and checks code line is

for (var x = 0; x < searchParams.length; x++) {
     var container = $('#checkBox');
     var inputs = container.find('input');
     var id = inputs.length + 1;
     var inputName = searchParams[x].name;
     $('<textarea />', { id: inputName, name: inputName, placeholder: inputName, rows: "2", class: "search-area-txt col-sm-12" }).appendTo(searchbox);
     $('<input />', { type: 'checkbox', id: 'x' + id, name: inputName }).appendTo(checkBox);
     $('<label />', { 'for': 'x' + id, text: inputName, id: inputName, name: inputName }).appendTo(checkBox);
}

But this will need to be saved in the localStorage so when refresh it will persist the input to be hidden when its exists in the localStorage.

Edit: the code below should save the name in the localStorage in array form.

var inputNames = [];
getChk(id){
 var indexOfItem = inputNames.indexOf(name)
 if (indexOfItem >= 0) {
    inputNames.splice(indexOfItem, 1);
 } else {
    inputNames.push(name);
 }
 localStorage.setItem('chked', JSON.stringify(inputNames))
}

My attempt is by adding .click(function(){})

$('<input />', { type: 'checkbox', id: 'x' + id, name: inputName }).appendTo(checkBox).click(function(){
 getChk(id); // only gives me the id name
});

HTML inputs and checkbox html enter image description here

0

2 Answers 2

3

The issue is because when the click event handler runs the for loop has completed, therefore the id variable holds the last value.

To fix this, amend your click handler to read the id attribute directly from the element which raised the event:

$('<input />', { type: 'checkbox', id: 'x' + id, name: inputName }).appendTo(checkBox).click(function() {
   getChk(this.id);
});

Also, as spotted by @guest271314, the correct method when setting localStorage data is setItem(), not set():

localStorage.setItem('checked', id);
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah sorry I meant to say .setItem, oh right will try adding this
but how can I correspond the checkbox to the textarea, just reliased that this.id will only give me the id of the checkbox not the textarea
You could store a data attribute on all the grouped elements and select by that, or use DOM traversal depending on how they appear in the page.
is it possible to show how? I've added a screenshot of the html structure of the inputs/checks
0

you can try below code

getChk(e){
 localStorage.set('checked', this.id);
 console.log('input id>> ', this.id);
}

.click(getChk);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.