#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,481
    Rep Power
    221

    Is this right to update the attr with Jquery like this?


    Hi;

    This isn't working.

    Opens the modal, alerts the right id, however, the value of attribute 'data-plc_id' doesn't change.

    Code:
    <button onclick="update_script_id(<?php echo $row['plc_id'];?>);" type="button" class="btn btn-info btn-sm"  data-toggle="modal" data-target="#modal_links" ><i class = "fa fa-link"></i> Links</button>
    Code:
    <script>
    function update_script_id(id)
    	{
    		alert(id);
    		$("#broadcast_plc_script").attr('data-plc_id', id);
    	}	
    </script>
    Code:
    <div class="modal" tabindex="-1" role="dialog" id="modal_links">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">Javascript Code:</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
           <span class="badge badge-info">Javascript Code:</span></h5>
          <textarea id = "code" class="form-control" style="background-color: #fcfcde; font-size: 80%" rows="4"><script id="broadcast_plc_script" type="text/javascript" src="<?php echo base_url('broadcast_plc_js');?>" data-launch_owner_email_hashed="<?php echo $launch_hashed_email;?>" data-plc_id="" ></script>
        </textarea>
          </div>
          <div class="modal-footer">
            <button style="margin-top: 5px;" type="button" class="btn btn-danger btn-sm" onclick="copy_to_clipboard()">Copy Code</button>
          </div>
        </div>
      </div>
    </div>
    <!---->
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,752
    Rep Power
    9646
    It should update. Did you look at the DOM to see if maybe it really did update and it's just that you expect your script to have picked up on the change after it's already executed?
  4. #3
  5. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,253
    Rep Power
    4150
    Your script tag is inside a <textarea> meaning it's not part of the DOM and can't be accessed like that. Instead it's just plain text in that textarea's value.
    Note that when you're putting HTML into a textarea like that, you should be escaping it.
    Code:
    <textarea id = "code" class="form-control" style="background-color: #fcfcde; font-size: 80%" rows="4">
    &lt;script id="broadcast_plc_script" type="text/javascript" src="<?php echo base_url('broadcast_plc_js');?>" data-launch_owner_email_hashed="<?php echo $launch_hashed_email;?>" data-plc_id=""&gt;&lt;/script&gt;
    </textarea>
    If you want to update that attribute, you'll need to read the textarea's value, do a string replacement, then update the textarea's value.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo