Reputation: 37
I want to swap the text inside a html button, but using a function with 3 parameters, id, first_text, second_text.
I was looking on another questions with the same problem, but it won't help me.
This is not working:
Button(var1,value,id are variables from php):
<input type='button' id=".$id." onclick='swaptxt('"$.id."', '".$var1."','".$value."')' value=".$value.">
Function:
function swaptxt(index, text1, text2){
var elem = document.getElementById(index);
if (elem.value===text1)
elem.value = text2;
else
elem.value = text1;
Upvotes: 0
Views: 134
Reputation: 564
You are not getting the correct output because you are passing the wrong id in argument. In input, you have given id as ".$id." but in the argument, you are passing the id as "$.id.". Provide the correct id and you will get your expected output.
function swaptxt(index, text1, text2){
var elem = document.getElementById(index);
if (elem.value===text1)
elem.value = text2;
else
elem.value = text1;
}
<input type='button' id=".$id." onclick='swaptxt(".$id.",".$value.", ".$var1.")' value=".$value">
Upvotes: 2
Reputation: 2761
another way to concat it:
onclick="swaptxt(\'' . $id . '\',\'' . $var1 . '\',\'' . $value .'\');"
Upvotes: 0
Reputation: 5191
Don't echo it from PHP, just drop out of PHP and only use PHP to add the variables.
?> <!-- we are no longer in PHP -->
<input type='button' id='<?php echo $id; ?>'
onclick='swaptxt('<?php echo $id; ?>',
'<?php echo $var1; ?>',
'<?php echo $value; ?>')'
value='<?php echo $value; ?>'>
<?php // back to PHP now
Upvotes: 1