james bond
james bond

Reputation: 117

Set value from text replace

My js error,

I want set value from text replace to input value

this html code :

<td><span>5</span> <b>Stock</b><br /></td>
<input type="text" name="qty" size="5" maxlength="5" class="pcs" value="0" /> <b>Stock</b>

my js code :

var curInnerHTML = document.body.innerHTML;
curInnerHTML = curInnerHTML.replace(/5/g,"11");
document.body.innerHTML = curInnerHTML;

var settotal    = document.querySelector("[class='pcs']");
var stock       = document.body.innerHTML;
settotal.value  = stock; // need 11

or http://jsfiddle.net/james007/4X68k/

Upvotes: 0

Views: 108

Answers (1)

JJJ
JJJ

Reputation: 33163

That's a bit convoluted way to do what you probably want to accomplish. It's better to just pick the text you want instead of the entire page's content. Give the span that contains the number an id:

<td><span id="stock">5</span> <b>Stock</b><br /></td>

Then target the span specifically:

document.getElementById( 'stock' ).innerText = '11';

var settotal = document.querySelector("[class='pcs']");
settotal.value = document.getElementById( 'stock' ).innerText;

Although depending on what you're doing it'd be better to not use document contents as a data store at all:

var stock = 11;
document.getElementById( 'stock' ).innerText = stock;

var settotal = document.querySelector("[class='pcs']");
settotal.value = stock;

Upvotes: 1

Related Questions