javascript - How do I pass e.target back to the server -


i have front-end track element has been clicked , send server work on backend depending on element is. code setup so...

$('body').on('click', function(e) {     $.post( '/edit', {el: $( e.target ).serialize()}, function(response) {         console.log( response );     }); }); 

but el empty string on server. else can e.target info server?

update:

i think question benefit context.

the basic function of app enable in-page editing. node server loads html page want edit. clicking on element on page lets me change text in element, posted node server, use cheerio module change dom representation , overwrite original html file. reloading page gives me new version of page edits had made.

but apply edits have made on front-end, cheerio needs e.target find right element in dom representation , change text, since many of elements on page don't have ids.

this whole app...

var     express  = require( 'express' ) ,   fs       = require( 'fs' ) ,   cheerio  = require( 'cheerio' ) ,   $        = '' ,   app      = express() ,   html     = '' ,   injected = "<script> \                     $( '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 );  \                         });  \                     });  \                 </script>";  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('</body>', injected + '</body>') )     }) })  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 changes!' )     }) })  app.listen( 8080 ) 

i run app:

node cms.js "e:\dropbox\sites\index.html" 

theoretically, should let me edit index.html "in-page" , without code editor. getting e.target server intact remains hurdle.

workaround:

my current workaround post entire html of page using $( 'html' ).html() regardless of element clicked, can new state of page in it's entirety , overwrite existing file new state. have browser extensions inject own html/js , want avoid painful process of stripping away before saving file. that, need tell cheerio element has been clicked.

serialize works on forms , generates output similar query string be, single=single&multiple=multiple&multiple=multiple3&check=check2&radio=radio1

you may want check this answer, use here html of button when it's clicked:

html

<input id="b" type="button" value="click" />  

js

$("#b").click (function (e) {     window.alert($(e.target).outerhtml()); });  jquery.fn.outerhtml = function(s) {     return s         ? this.before(s).remove()         : jquery("<p>").append(this.eq(0).clone()).html(); }; 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -