Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Thanked 0 Times in 0 Posts

    Swapping out tag info with javascript onclick dynamically inside a php loop

    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,

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Behind the Wall
    Thanked 361 Times in 357 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts