1

I have created a dynamic stock sheet that loads a table that contains list of products, one of the feilds is a html check box, I have linked the checkbox to a javascript function to using the onclick event and passing the product ID in the function().

When the Product ID is just a number such as 12345, the Javascript works fine, but when the ID is a mixed string such as 12345-HIGH then the Javascript fails. I need to keep the ID intact because it is used later on so I cannot change it or convert it?

I cant seem to find anyway of passing a mixed string to a javascript function. I'm not sure if this helps but the ID will normally have a '-' in it and the text, if any will always be different.

4
  • 5
    Please post the code for this function and the code that calls it. Commented Apr 12, 2013 at 14:26
  • there's no such thing as a "mixed string" in JS. chances are the code is simply expecting a number, and is using numeric comparisons on the supplied parameter. Commented Apr 12, 2013 at 14:27
  • I don't believe there is any such thing as "mixed strings" in js. You have plain strings. If you are retrieving product id from element property it should be a string even if it is just composed of numbers Commented Apr 12, 2013 at 14:27
  • I'm not very familiar with Javascript, the script is not expecting a number only because it will work with text but when mixed text and numbers are sent it seems to fail, its either one or the other but there is no code to say that.. Commented Apr 12, 2013 at 14:45

2 Answers 2

1

It's hard to know the right answer without knowing what every possible string is composed of, but it sounds like it will

  1. always start with a number
  2. sometimes have a - followed by a string

If those are true, you can do this:

var productId = "12345-HIGH";
var num = productId.split('-')[0];

It's still a string at this point, but you can convert it to an integer with parseInt(num), as jxpx777 posted.

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

Comments

1

If you know that your string will always begin with the number portion you want to extract, you can use parseInt() and exploit the nature of Javascript parsing to extract just the numeric portion:

parseInt("12345-HELLO")

will return 12345 as an integer. But

parseInt("HELLO-12345-HELLO")

will return NaN

1 Comment

Is there anyway of keeping the whole string because it is then passed to a PHP page which checks a database you see, so I require it to be exactly the same, The script doesn't even want to run when text is involved.

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.