user435704
user435704

Reputation: 1

split & showing value in javascript

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

Upvotes: 0

Views: 576

Answers (4)

hpuiu
hpuiu

Reputation: 96

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

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

Upvotes: 0

mck89
mck89

Reputation: 19231

Use the spli method:

var itemid=document.getElementById('item_code').value.split("*")[0];

Upvotes: 0

SLaks
SLaks

Reputation: 887365

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('*'));

Upvotes: 1

Gordon
Gordon

Reputation: 316969

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

Upvotes: 0

Related Questions