-1

I have a table in a form with a list of products where there is an input for each (let's call it 'pc'). Each input is named as 'pc' and product id with an id as the same. IE: 'pc100', 'pc101', etc. Each input has an onkeyup event to call a function to populate a price where I pass the product id (onkeyup='myfunction(100) The function receives the id but I cannot configure the javascript to that id. I will have the rest of the code as long as I can get the dynamic variable name. The internet is full of all kinds of things like eval() and window[] and I have tried every possible combination I can think of/find. I know it's possible to get this to work. So...how can I get this alert to work?

Most the results I will give me 'pc100' when I need 'Heres my test'

function myfunction(idpro) {
    var pc331 = 'Heres my test';
    alert( "pc" + idpro );

}
8
  • You cannot access locally scoped variables with a variable name. That can only be used on object properties, and it would be done differently to what you're trying. Commented Jun 19, 2019 at 19:14
  • eval("pc"+idpro) should work. Commented Jun 19, 2019 at 19:15
  • 5
    @Jeff Suggesting eval without issuing a strong warning of using it is really bad advice. Commented Jun 19, 2019 at 19:16
  • 4
    An alternative would be to have an object or array named pc and do pc[idpro] Commented Jun 19, 2019 at 19:16
  • @CrayonViolent Given the example we have it would be obj['pc'+idpro]. Commented Jun 19, 2019 at 19:17

1 Answer 1

2

Don't use dynamic variable names. Do use look-ups in data structures:

function f(id) {
  var pc = {
    331: "Here's my test"
  }

  console.log(pc[id]);
}

Dynamic variables are a huge problem, especially from a security perspective. If you were naive and used eval someone could construct an id value that executes arbitrary JavaScript and then you have an XSS problem.

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

10 Comments

Basically I can grab the data I need and want to put it into the price with that id. IE: document.getElementById(pc331).value=pc331;
What is pc311 in this case? A simple string? Also consider using $('#' + pc311).val(pc311) if you have jQuery-like selectors. You're going to have to explain your use case in more detail because it's really hazy right now. Your example text of 'Heres my test' is not a valid CSS ID.
Input is name='pc311' id='pc311' and populates with a price of 129.00. If the customer is buying more than 5 there is a price in the database that the function can grab and put into that input. Let's say the volume price is 119.00. The form populates with all products ordered over the last 3 months so there could be 20-50 products each with a price and quantity. Hence, each product has an input called qty + idpro and pc + idpro. When there is a change in quantity and over 5 the function needs to get the new price and put it into the correct pc input.
However, currently I have the 'pc' and the id (311) but cannot get them put together to update input 'pc311'
'pc' + id gives you that string. Not sure what else you're asking for.
|

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.