Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I pass e.target back to the server
    primarykey
    data
    text
    <p>I have a front-end where I track what element has been <code>click</code>ed and send it back to the server to do some work on the backend depending on what that element is. The code is setup like so...</p> <pre><code>$('body').on('click', function(e) { $.post( '/edit', {el: $( e.target ).serialize()}, function(response) { console.log( response ); }); }); </code></pre> <p>But I get <code>el</code> as an empty string on the server. What else can I do to get the <code>e.target</code> info to my server?</p> <p><strong>Update:</strong></p> <p>I think my question could benefit from some context. </p> <p>The basic function of the app is to enable in-page editing. A node server loads the HTML page I want to edit. Clicking on any element on this page lets me change the text in that element, which is then <code>POST</code>ed back to the node server, where I use the <code>cheerio</code> module to change the DOM representation and overwrite the original HTML file. Reloading the page now gives me the new version of the page with the edits I had made. </p> <p>But to apply the edits I have made on the front-end, <code>cheerio</code> needs the <code>e.target</code> to find the right element in its DOM representation and then change the <code>text</code>, since many of the elements on the page don't have <code>id</code>s. </p> <p>This is the whole app...</p> <pre><code>var express = require( 'express' ) , fs = require( 'fs' ) , cheerio = require( 'cheerio' ) , $ = '' , app = express() , html = '' , injected = "&lt;script&gt; \ $( 'body').on( 'click', function(e) { \ $( e.target ).text( prompt('Enter new value:') ); \ $.post( '/edit', {el: $(e.target).serialize(), newVal: $(e.target).text()}, function(response) { \ alert( response ); \ }); \ }); \ &lt;/script&gt;"; app.use( express.static(__dirname) ) app.use( express.bodyParser() ) app.get( '/', function( req, res ) { fs.readFile( process.argv[2], 'utf8', function(err, data) { $ = cheerio.load( data ) err? console.log( err ): res.send( data.replace('&lt;/body&gt;', injected + '&lt;/body&gt;') ) }) }) app.post( '/edit', function(req,res) { $( req.body.el ).text( req.body.newVal ) fs.writeFile( process.argv[2], $.html(), function(err) { err? res.send( err ): res.send( 'file saved with changes!' ) }) }) app.listen( 8080 ) </code></pre> <p>I then run the app:</p> <pre><code>node cms.js "E:\Dropbox\sites\index.html" </code></pre> <p>Theoretically, this should let me edit <code>index.html</code> "in-page" and without a code editor. But getting the <code>e.target</code> back to the server intact remains the hurdle.</p> <p><strong>Workaround:</strong></p> <p>My current workaround is to just <code>POST</code> the entire HTML of the page using <code>$( 'html' ).html()</code> so regardless of which element is clicked, I can get the new state of the page in it's entirety and overwrite the existing file with this new state. But I have browser extensions that inject their own HTML/JS and I want to avoid the painful process of stripping those away before saving to file. For that, I need to tell <code>cheerio</code> exactly which element has been <code>click</code>ed.</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