Question

Below is how I made an alert showing the "dynamic" id using onblur. I have a while loop with input fields so the id is pulling from a unique id in a database.

How would I get the value too? Normally I would just set a variable like usual but it's in a loop so I'm not understanding how.

<script type="text/javascript">
    function doUpdateEntries(clicked_id) {
        alert(clicked_id);
        // how do I get the value AND id?
    }
</script>

while ($row = mysql_fetch_assoc($result)) {
    <input onblur="doUpdateEntries(this.id);" id=".$row['entry_id']." name=".$row['entry_id']." type="text" value=".$row['reviews'].">
}
Was it helpful?

Solution

You could simply pass through the value to the function doUpdateEntries as another parameter.

<script type="text/javascript">
    function doUpdateEntries(id, value) {
        alert(id);
        alert(value);
    }
</script>

while ($row = mysql_fetch_assoc($result)) {
    <input onblur="doUpdateEntries(this.id, this.value);" id=".$row['entry_id']." name=".$row['entry_id']." type="text" value=".$row['reviews'].">
}

OTHER TIPS

As Ankit mentions… just pass in this, à la

<input onblur="doUpdateEntries(this);" id=".$row['entry_id']." name=".$row['entry_id']." type="text" value=".$row['reviews'].">

Below is how I made an alert showing the "dynamic" id using onblur. I have a while loop with input fields so the id is pulling from a unique id in a database.

How would I get the value too? Normally I would just set a variable like usual but it's in a loop so I'm not understanding how.

function doUpdateEntries(_this) {
    var _id = _this.id;
    var _name = _this.value;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top