...

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



jdgower
09-04-2010, 04: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']; ?>"/>
</div>


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,
Jeremy

Dormilich
09-04-2010, 04: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum