Jump to content

is this a concat error?


StevenOliver

Recommended Posts

This hardcoded "999" works fine:
function changeAmount(a){
document.form1.sku999.value="44";
}

But since the number is a php variable, I need this to work. It does not work:
function changeAmount(a){
document.form1.'sku'+a.value="44";
}

In the html below, I "hardcoded" the php variable below to "999" just to try and make the simple javascript above to work.... but it doesn't.

<input onClick="changeAmount('999');" type="button">
<input type="text" name="sku999">

I've spent days trying to learn javascript concatenation, but even this doesn't work:

function changeAmount(a){
var conc = 'sku'+a;
document.form1.conc.value="44";
}

If you would please let me know what I've done wrong. Thank you.

Link to comment
Share on other sites

I don't see any PHP in there. I do see Javascript though. Probably means this question will do better in the Javascript forum.

 

document.form1.sku999.value="44";

sku999 is a name. It is not a variable. You cannot stick a variable in there because Javascript is only going to look at its name.

document.form1.'sku'+a.value="44";

I don't know why you think you can just stick a string in there. There are rules to how this kind of stuff works. Follow the rules.

document.form1.conc.value="44";

Better except that "conc" is a variable, and like I said earlier, Javascript will only look at the name. And form1 doesn't have a "conc".

 

Fortunately there is a way to access an object member using the value of a variable or expression. Square brackets. Just like arrays.

document.form1['sku'+a].value="44";

 

Link to comment
Share on other sites

Try accessing via id

<!DOCTYPE>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample</title>
<script type="text/javascript">
    
    function changeAmount(a)
    {
        var sku = document.getElementById("sku"+a)
        sku.value = 44
    }
    
</script>
</head>
<body>

SKU 999 : <input name='sku999' id='sku999' value = ''><br>
<button id='mybtn' onclick='changeAmount(999)'>Set value</button>
</body>
</html>

 

Link to comment
Share on other sites

requinix, thank you! In days of searching, I never found the brackets suggestion.
Also, I'll give more attention to the correct forum topic (or, at least I should have put <?php echo..." on top LOL )

Barand, thank you!
The html is a result of a PHP loop through 100+ mySQL records, and since I already have "<textarea name=sku<?=$number?>...." at least 100+ times, I thought it would "bog down the html page size" if I additionally added id="sku<?=$number?>" to all 100+ textareas. (I still think in terms of the 1990's browsers where you had to keep an eye on each extra unnecessary byte...

Link to comment
Share on other sites

10 minutes ago, StevenOliver said:

since I already have "<textarea name=sku<?=$number?>...." at least 100+ times

That form is going to be one major headache when it's posted. Use array syntax when naming your fields then processing them is just a loop,

<textarea name='sku[$number]' >

 

Link to comment
Share on other sites

It's a loop like this:

while ($sku = $query->fetch_assoc()) { // a hundred results
echo '<textarea name="'.$sku["product_name"].' ..... etc.

And that produces a huge amount of html.

I meant that if I added an id tag to that, it would almost double the amount of html sent to the browser:
echo '<textarea name="'.$sku["product_name"].' id="'.$sku["product_name"].'"... etc. //

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.