View Full Version : Swapping out tag info with javascript onclick dynamically inside a php loop

09-04-2010, 05:03 PM
Below is the code.

My function:

function swap_content(id1,id2)
var tmp = document.getElementById(id1).name;
var theval = document.getElementsByName('primary_propertyp_id')[0].value;
document.getElementById(id1).name = document.getElementsByName('primary_propertyp_id')[0].name;
document.getElementsByName('primary_propertyp_id')[0].name = tmp;
document.getElementById(id2).id = document.getElementById('primary').id;
document.getElementById('primary').id = 'addressid_'+theval+'_div';

Applicable Code for primary info:

<div id="primary">
<label>primary to secondary:</label><input name="primary_propertyp_id" type="text" id="addressid_<? echo $row['primary_property_id']; ?>" value="<? echo $row['primary_property_id']; ?>"/>

Applicable Code for secondary info/link to change:

echo "<div id=\"addressid_" . $properties_row['id'] . "_div\">";
echo "<a href=\"#\" onclick=\"swap_content(&apos;addressid_" . $properties_row['id'] . "&apos;,&apos;addressid_" . $properties_row['id'] . "_div&apos;); return false\">Make Primary</a>";
echo "<input id=\"addressid_" . $properties_row['id'] . "\" name=\"addressids[]\" value=\"" . $properties_row['id'] . "\" type=\"text\">";
echo "</div>";

This is running through a PHP loop so it's making multiple divs and links for the secondaries. I am wanting to be able to swap out any of them to make them 'primary'....this works for the first click, but after the first click it makes every div id and input name the same as the first that was clicked.

It's also not working AT all if i click on the bottom link first, then a link above it. Top-down works, bottom-up doesn't...Please help :(

Thanks in advance,

09-04-2010, 05:12 PM
you know that PHP and JavaScript don't run at the same time?

once you print with PHP the IDs in the JavaScript call, those are unlikely to change, thus the function is always executed with the same parameters.