Reputation: 1
I'am trying to pass the value of the textbox (id = msg) to js but the output is always the first textbox value on the foreach loop. Please Help me. Here is the code.
JS:
function sub() {
var name = document.getElementById('msg').value;
alert(name);
}
HTML:
<?php
foreach($messages->result() as $msg):
foreach($agentname->result() as $agnt):
if($msg->message_status == 1):
$message_status = "<font class='icon-exclamation-sign'></font> <font class='purple'>New message!</font>";
else:
$message_status = "";
endif;
?>
<div class="accordion-wrapper" style="margin-top:0">
<a id="message" onclick="return sub();" rel="msg<?php echo $msg->message_id;?>" id = "button_id" style="background-color:#C2E4CD" href="javascript:void(0)" class="accordion-title blue"><span><?php echo $message_status; ?> <font class="icon-comment"></font> <font class="orange" >From:</font> <?php echo $agnt->agent_shortname;?> | <font class="icon-envelope-alt"></font> <font class="orange">Subject:</font> <?php echo $msg->message_title;?></span></a>
<div class="accordion-content">
<input type="text" id="msg" value="<?php echo $msg->message_id;?>" />
<p><?php echo $msg->message;?></p>
</div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
Upvotes: 0
Views: 188
Reputation: 10141
I have added name
attribute to your input
and a button clicking on which it will display the values of the inputs one by one, so now you can try this code
JS:
function check() {
var inputs = document.getElementsByName('messages[]');
alert(inputs.length);
for (var x = 0; x < inputs.length; x++) {
inp_val = inputs[x].value;
alert(inp_val);
}
}
HTML :
<?php
foreach($messages->result() as $msg):
foreach($agentname->result() as $agnt):
if($msg->message_status == 1):
$message_status = "<font class='icon-exclamation-sign'></font> <font class='purple'>New message!</font>";
else:
$message_status = "";
endif;
?>
<div class="accordion-wrapper" style="margin-top:0">
<a id="message" onclick="return sub();" rel="msg<?php echo $msg->message_id;?>" id = "button_id" style="background-color:#C2E4CD" href="javascript:void(0)" class="accordion-title blue"><span><?php echo $message_status; ?> <font class="icon-comment"></font> <font class="orange" >From:</font> <?php echo $agnt->agent_shortname;?> | <font class="icon-envelope-alt"></font> <font class="orange">Subject:</font> <?php echo $msg->message_title;?></span></a>
<div class="accordion-content">
<input type="text" id="msg" name="messages[]" value="<?php echo $msg->message_id;?>" />
<p><?php echo $msg->message;?></p>
</div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
<input type="button" value="check" onclick="check()">
Upvotes: 0
Reputation: 23958
You are putting value of outer loop in the textbox.
As per my knowledge, you should put inner loop's value in the textbox.
<input type="text" id="msg" value="<?php echo $msg->message_id;?>" />
Did you mean $agnt
instead of $msg
?
Upvotes: 1