tags:

views:

30

answers:

4

i have one variable in javascript named itemid i.e. var itemid=document.getElementById('item_code').value;

in item_code there is value in this format "1025*1" and i want to display only 1025 not whole in javascript div

plz give me some idea, it is same as explode() in php but i want this in javascript to display in div

A: 

You can use

  • parseInt - Parses a string argument and returns an integer of the specified radix or base.

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

Example:

parseInt("1025*1", 10); // 1025
Gordon
+1  A: 

You're looking for the split method.

For example:

var array = value.split('*');
var first = array[0];

Alternatively,

var first = value.substring(0, value.indexOf('*'));
SLaks
A: 

Use the spli method:

var itemid=document.getElementById('item_code').value.split("*")[0];
mck89
thank u very much............... it works...................
A: 

This will display "1025" (will strip *1) into DIV with ID="DIV_ID":

document.getElementById('DIV_ID').innerHTML = itemid.split(/(\d+)\*\d+/)[1];
hpuiu