3

I am trying to create ID dynamically in the HTML object and use of getElementById() in my javascript to access the HTML input value based on the button I clicked and insert into their respective HTML Select list.

My HTML snippets:

<input type="text" id="addDesc1"><input type="button" value="Add" onclick="addDescText(1)"> 
<input type="text" id="addDesc2"><input type="button" value="Add" onclick="addDescText(2)">
....
....
<select id="desc1">....</select>
<select id="desc2">....</select> 

My javascript snippets:

function addDescText(id) {
    var descText = document.getElementById("addDesc".concat(id)).value;
    var selList = document.getElementById("desc".concat(id));
    ....
    ....
    some javascript to add the respective description to their respective select list
    ....
}
3
  • 4
    don't use "desc".concat(id). Do either "desc" + id , or `desc${id}` Commented Dec 10, 2019 at 14:37
  • document.getElementById("addDesc"+id) or with es2015's template variables like document.getElementById(addDesc${id}) Commented Dec 10, 2019 at 14:37
  • 1
    You could do this by getting the element that performed the click (this will be available through the event argument). Then once you have that getting the other elements can be done using accessing adjacent siblings. Commented Dec 10, 2019 at 14:40

2 Answers 2

1

concat() is an array method, you can not use that on string. Simply use + to concatenate the parameter with the string.

Demo:

function addDescText(id) {
  var descText = document.getElementById("addDesc"+id).value;
  var selList = document.getElementById("desc"+id);

  console.log(descText);
  console.log(selList);
}
<input type="text" id="addDesc1"><input type="button" value="Add" onclick="addDescText(1)"> 
<input type="text" id="addDesc2"><input type="button" value="Add" onclick="addDescText(2)">

<select id="desc1">....</select>
<select id="desc2">....</select>

Sign up to request clarification or add additional context in comments.

1 Comment

I tried that earlier but it did not worked.. I had tried again and confirmed it did not worked for the 2nd or 3rd "Add" button. The first one will work.
1

I would encourage you to make use of the event parameter that is passed to all event handlers (on-click-event in your case) and add that handler programmatically.

A possible solution would be

HTML

  <input type="text" id="text1">
  <input type="button" value="Add" class="add-desc-button" data-target="1">

JS

// get all buttons
let allButtons = document.querySelectorAll('.add-desc-button')

// add event handler
for (let i=0; i<allButtons.length; i++) {
  allButtons[i].addEventHandler('click', addDescriptionHandler)
}

// event handler
function addDescriptionHandler(event) {
   // retrieve the number you passed in before like this
   let number = event.target.getAttribute('data-target')
   // ... your code here
}

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.