Note that there are some explanatory texts on larger screens.

plurals
  1. POUse jQuery replaceWith(); on specific class
    primarykey
    data
    text
    <p>I have a table of contacts that is populated via PHP/MySQL. Each contact is in its own line, and its class name is "line," plus the row number, e.g. "line01" or "line02."</p> <p>I have a jQuery modal dialog that I'm using to add new contacts, edit current contacts, and delete contacts. I am using <code>.replaceWith()</code> for editing and <code>.append();</code> for adding new contacts. My challenge now is to get the edit function working correctly so that it only replaces the <code>&lt;div&gt;</code> row that is being edited, and not all of the rows. </p> <p>How do I select the particular element to replace, or make sure that the row being replaced has the same ID of the row that is being edited? Should be simple enough, but I just haven't been able to get it working correctly. Help is appreciated!</p> <p>Here is the code I have so far: </p> <pre><code>$("#contact-dialog").dialog({ autoOpen: false, width: 600, modal: true, buttons: { "Add Contact": function() { var bValid = true; allFields.removeClass( "ui-state-error" ); bValid = bValid &amp;&amp; checkLength( contact_first_name, "first name", 2, 64 ); bValid = bValid &amp;&amp; checkLength( contact_last_name, "last name", 2, 64 ); bValid = bValid &amp;&amp; checkLength( title, "title", 1, 64 ); bValid = bValid &amp;&amp; checkLength( phone, "phone", 6, 30 ); bValid = bValid &amp;&amp; checkLength( email_address, "email_address", 5, 128 ); /* Handle edit function */ if (crow.val().length &gt; 0) { if ( bValid ) { if (contact_count % 2) { //$( "#contacts" ).html( // '&lt;div class="line'+contact_count+'"&gt;' + $('.line'+contact_count).replaceWith( '&lt;div class="action gray"&gt;' + '&lt;a href="javascript:edit_contact('+contact_count+');"&gt;&lt;img src="images/icons/edit.png" width="15" height="16" border="0" /&gt;&lt;/a&gt;' + '&lt;a href="javascript:delete_contact('+contact_count+');"&gt;&lt;img src="images/icons/trash.png" width="11" height="16" border="0" /&gt;&lt;/a&gt;' + '&lt;/div&gt;' + '&lt;div class="contact gray"&gt;&lt;input type="hidden" id="contact-first-name'+contact_count+'" name="contact_first_name[]" value="'+contact_first_name.val()+'" /&gt;' + '&lt;input type="hidden" id="contact-last-name'+contact_count+'" name="contact_last_name[]" value="'+contact_last_name.val()+'" /&gt;'+contact_first_name.val()+' '+contact_last_name.val()+'&lt;/div&gt;' + '&lt;div class="phone gray"&gt;&lt;input type="hidden" id="cphone'+contact_count+'" name="cphone[]" value="'+phone.val()+'" /&gt;'+phone.val()+'&lt;/div&gt;' + '&lt;div class="email gray"&gt;&lt;input type="hidden" id="email-address'+contact_count+'" name="email_address[]" value="'+email_address.val()+'" /&gt;'+email_address.val()+'&lt;/div&gt;' + '&lt;div class="primary gray"&gt;&lt;input type="radio" id="primary'+contact_count+'" name="primary[]" value="1" /&gt;&lt;/div&gt;' + '&lt;input type="hidden" id="twitter'+contact_count+'" name="twitter[]" value="'+twitter.val()+'" /&gt;' + '&lt;input type="hidden" id="facebook'+contact_count+'" name="facebook[]" value="'+facebook.val()+'" /&gt;' + '&lt;input type="hidden" id="linkedin'+contact_count+'" name="linkedin[]" value="'+linkedin.val()+'" /&gt;' + '&lt;input type="hidden" id="title'+contact_count+'" name="title[]" value="'+title.val()+'" /&gt;' + '&lt;/div&gt;' + '&lt;div class="clear"&gt;&lt;/div&gt;'); $( this ).dialog( "close" ); } else { //$( "#contacts" ).html( // '&lt;div class="line'+contact_count+'"&gt;' + $('.line'+contact_count).replaceWith( '&lt;div class="action"&gt;' + '&lt;a href="javascript:edit_contact('+contact_count+');"&gt;&lt;img src="images/icons/edit.png" width="15" height="16" border="0" /&gt;&lt;/a&gt;' + '&lt;a href="javascript:delete_contact('+contact_count+');"&gt;&lt;img src="images/icons/trash.png" width="11" height="16" border="0" /&gt;&lt;/a&gt;' + '&lt;/div&gt;' + '&lt;div class="contact"&gt;&lt;input type="hidden" id="contact-first-name'+contact_count+'" name="contact_first_name[]" value="'+contact_first_name.val()+'" /&gt;' + '&lt;input type="hidden" id="contact-last-name'+contact_count+'" name="contact_last_name[]" value="'+contact_last_name.val()+'" /&gt;'+contact_first_name.val()+' '+contact_last_name.val()+'&lt;/div&gt;' + '&lt;div class="phone"&gt;&lt;input type="hidden" id="cphone'+contact_count+'" name="cphone[]" value="'+phone.val()+'" /&gt;'+phone.val()+'&lt;/div&gt;' + '&lt;div class="email"&gt;&lt;input type="hidden" id="email-address'+contact_count+'" name="email_address[]" value="'+email_address.val()+'" /&gt;'+email_address.val()+'&lt;/div&gt;' + '&lt;div class="primary"&gt;&lt;input type="radio" id="primary'+contact_count+'" name="primary[]" value="1" /&gt;&lt;/div&gt;' + '&lt;input type="hidden" id="twitter'+contact_count+'" name="twitter[]" value="'+twitter.val()+'" /&gt;' + '&lt;input type="hidden" id="facebook'+contact_count+'" name="facebook[]" value="'+facebook.val()+'" /&gt;' + '&lt;input type="hidden" id="linkedin'+contact_count+'" name="linkedin[]" value="'+linkedin.val()+'" /&gt;' + '&lt;input type="hidden" id="title'+contact_count+'" name="title[]" value="'+title.val()+'" /&gt;' + '&lt;/div&gt;&lt;div class="clear"&gt;&lt;/div&gt;'); $( this ).dialog( "close" ); } contact_count++; } } /* Handle new contact rows */ else if ( bValid ) { if (contact_count % 2) { $( "#contacts" ).append( '&lt;div class="line'+contact_count+'"&gt;' + '&lt;div class="action gray"&gt;' + '&lt;a href="javascript:edit_contact('+contact_count+');"&gt;&lt;img src="images/icons/edit.png" width="15" height="16" border="0" /&gt;&lt;/a&gt;' + '&lt;a href="javascript:delete_contact('+contact_count+');"&gt;&lt;img src="images/icons/trash.png" width="11" height="16" border="0" /&gt;&lt;/a&gt;' + '&lt;/div&gt;' + '&lt;div class="contact gray"&gt;&lt;input type="hidden" id="contact-first-name'+contact_count+'" name="contact_first_name[]" value="'+contact_first_name.val()+'" /&gt;' + '&lt;input type="hidden" id="contact-last-name'+contact_count+'" name="contact_last_name[]" value="'+contact_last_name.val()+'" /&gt;'+contact_first_name.val()+' '+contact_last_name.val()+'&lt;/div&gt;' + '&lt;div class="phone gray"&gt;&lt;input type="hidden" id="cphone'+contact_count+'" name="cphone[]" value="'+phone.val()+'" /&gt;'+phone.val()+'&lt;/div&gt;' + '&lt;div class="email gray"&gt;&lt;input type="hidden" id="email-address'+contact_count+'" name="email_address[]" value="'+email_address.val()+'" /&gt;'+email_address.val()+'&lt;/div&gt;' + '&lt;div class="primary gray"&gt;&lt;input type="radio" id="primary'+contact_count+'" name="primary[]" value="1" /&gt;&lt;/div&gt;' + '&lt;input type="hidden" id="twitter'+contact_count+'" name="twitter[]" value="'+twitter.val()+'" /&gt;' + '&lt;input type="hidden" id="facebook'+contact_count+'" name="facebook[]" value="'+facebook.val()+'" /&gt;' + '&lt;input type="hidden" id="linkedin'+contact_count+'" name="linkedin[]" value="'+linkedin.val()+'" /&gt;' + '&lt;input type="hidden" id="title'+contact_count+'" name="title[]" value="'+title.val()+'" /&gt;' + '&lt;/div&gt;&lt;div class="clear"&gt;&lt;/div&gt;'); $( this ).dialog( "close" ); } else { $( "#contacts" ).append( '&lt;div class="line'+contact_count+'"&gt;' + '&lt;div class="action"&gt;' + '&lt;a href="javascript:edit_contact('+contact_count+');"&gt;&lt;img src="images/icons/edit.png" width="15" height="16" border="0" /&gt;&lt;/a&gt;' + '&lt;a href="javascript:delete_contact('+contact_count+');"&gt;&lt;img src="images/icons/trash.png" width="11" height="16" border="0" /&gt;&lt;/a&gt;' + '&lt;/div&gt;' + '&lt;div class="contact"&gt;&lt;input type="hidden" id="contact-first-name'+contact_count+'" name="contact_first_name[]" value="'+contact_first_name.val()+'" /&gt;' + '&lt;input type="hidden" id="contact-last-name'+contact_count+'" name="contact_last_name[]" value="'+contact_last_name.val()+'" /&gt;'+contact_first_name.val()+' '+contact_last_name.val()+'&lt;/div&gt;' + '&lt;div class="phone"&gt;&lt;input type="hidden" id="cphone'+contact_count+'" name="cphone[]" value="'+phone.val()+'" /&gt;'+phone.val()+'&lt;/div&gt;' + '&lt;div class="email"&gt;&lt;input type="hidden" id="email-address'+contact_count+'" name="email_address[]" value="'+email_address.val()+'" /&gt;'+email_address.val()+'&lt;/div&gt;' + '&lt;div class="primary"&gt;&lt;input type="radio" id="primary'+contact_count+'" name="primary[]" value="1" /&gt;&lt;/div&gt;' + '&lt;input type="hidden" id="twitter'+contact_count+'" name="twitter[]" value="'+twitter.val()+'" /&gt;' + '&lt;input type="hidden" id="facebook'+contact_count+'" name="facebook[]" value="'+facebook.val()+'" /&gt;' + '&lt;input type="hidden" id="linkedin'+contact_count+'" name="linkedin[]" value="'+linkedin.val()+'" /&gt;' + '&lt;input type="hidden" id="title'+contact_count+'" name="title[]" value="'+title.val()+'" /&gt;' + '&lt;/div&gt;&lt;div class="clear"&gt;&lt;/div&gt;'); $( this ).dialog( "close" ); } contact_count++; } }, Cancel: function() { $( this ).dialog( "close" ); } }, close: function() { allFields.val( "" ).removeClass( "ui-state-error" ); } }); </code></pre> <p><strong>Edit:</strong> Here is the code for <code>edit_contact()</code>:</p> <pre><code>function edit_contact(contact_line, cid) { // $('#contact-dialog-link').val('edit'); $('#contact-dialog-id').val(cid); $('#contact-first-name').val($('.line'+contact_line+' #contact-first-name'+contact_line).val()); $('#contact-last-name').val($('.line'+contact_line+' #contact-last-name'+contact_line).val()); $('#title').val($('.line'+contact_line+' #title'+contact_line).val()); $('#cphone').val($('.line'+contact_line+' #cphone'+contact_line).val()); $('#email-address').val($('.line'+contact_line+' #email-address'+contact_line).val()); $('#twitter').val($('.line'+contact_line+' #twitter'+contact_line).val()); $('#facebook').val($('.line'+contact_line+' #facebook'+contact_line).val()); $('#linkedin').val($('.line'+contact_line+' #linkedin'+contact_line).val()); $('#contact-update').val(contact_line); $('#contact-dialog-link').trigger('click'); } </code></pre> <p><strong>Edit:</strong> Here is the PHP that handles the save function:</p> <pre><code>if ($_POST &amp;&amp; $_POST['type'] == 'add-to-contacts') { $fid = $_SESSION['facility_current']; if ($_POST['input'] == 'edit') { // if ($_POST['contact-update'] == ''){ $dbQuery = sprintf("UPDATE facility_contacts SET fid=%d, first_name='%s', last_name='%s', title='%s', phone='%s', email_address='%s', twitter='%s', facebook='%s', linkedin='%s' WHERE cid = %d;", $fid,$_POST['first_name'],$_POST['last_name'],$_POST['title'],$_POST['phone'],$_POST['email_address'],$_POST['twitter'],$_POST['facebook'],$_POST['linkedin'],$_POST['cid']); } else { $dbQuery = sprintf("INSERT INTO facility_contacts (fid, first_name, last_name, title, phone, email_address, twitter, facebook, linkedin) VALUES (%d,'%s','%s','%s','%s','%s','%s','%s','%s');", $fid,$_POST['first_name'],$_POST['last_name'],$_POST['title'],$_POST['phone'],$_POST['email_address'],$_POST['twitter'],$_POST['facebook'],$_POST['linkedin']); } $dbResult = $dbc-&gt;query($dbQuery,__FILE__,__LINE__); unapprove_proof_status(); echo ($dbResult) ? true : false; exit; </code></pre> <p>} </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload