user2413763
user2413763

Reputation: 81

PHP - Get the text associated with a button

i have problem like this.

-text | button-
-text | button-
(and so on)
-text | button-

== TEXTAREA ==

eg. i have 20 sets of text and its button. when I press any button, I get the text associated with the button that i pressed, and append its text to textarea. Here all pair of text and button using same id.

this is the code :

<div id="part" >
<form name='form1' id='form1'>
<div id="teks"> <?php echo $text; ?></div>
<button type="button" id="pos" name="pos" value="POS" onclick="addPOS();"/>POS</button>
</form>
</div>

<div id="part" >
<form name='form1' id='form1'>
<div id="teks"> <?php echo $text; ?></div>
<button type="button" id="pos" name="pos" value="POS" onclick="addPOS();"/>POS</button>
</form>
</div>

//and so on, up to 20 part text-button

<script type="text/javascript">
  function addPOS()
  {
      document.getElementById("posbox").value+='<?php echo $text; ?>';
  }
</script>
<div id="box" >
<textarea style="width:420px" name="posbox" id="posbox" rows="4" cols="70"></textarea>
</div>

I just want to add text (text beside button i press) to textarea. How to doing that?

any help will be greatly appreciated. thanks

Upvotes: 0

Views: 113

Answers (1)

Alex Kneller
Alex Kneller

Reputation: 413

you have to work with classes instead of ids. id must be unique . So get elements by class document.getElementsByClassName(''); and loop through array to change their values

Change everywhere id="" to class="" and do smth like this

var elements = document.getElementsByClassName('className');
for(var i=0; i<elements.length; i++) { 
  //your code elements[i].value=
}

Upvotes: 1

Related Questions