Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: Getting text with AJAX Date: Sat, 01 Jul 2017 12:10:24 +0100 Organization: A noiseless patient Spider Lines: 37 Message-ID: <871sq09zvj.fsf@bsb.me.uk> References: <31a6bc47-820d-4029-9ff1-c46617cf7e6f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="041c066391ca2b8f1f9730c78c417d85"; logging-data="4675"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/g3Oa5zMvM2bm8Sro/JW5l50ddEfGJS6o=" Cancel-Lock: sha1:b/s57IwJIs/aEPCh394vHP9HEH8= sha1:ZmEDeuNz+W49IKpCcQQ6h4f+k/8= X-BSB-Auth: 1.d3dbec290b0dcaa2675b.20170701121024BST.871sq09zvj.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:33567 bit-naughty@hotmail.com writes: > ...OK, so assume there's some text on the screen, like a headline, or > topic, say. When it's clicked, the actual text has to be got (from the > server), and shown. > What would the coding look like? > (assume the text itself is on a MySQL db on the server, with some kind > of "id" to it, which will be fetched by a PHP script - > ie. www.site.com/gettext.php?articleid=210). One possible outline: 1. The document could associate the ID with the headline using a data-* attribute:

Usenet not dead despite rumours

2. An click event handler is then associated with every headline. I usually do this on load: window.addEventListener('load', function () { var headlines = document.querySelectorAll('h1[data-article-id]'); for (var i = 0; i < headlines.length; i++) headlines[i].addEventListener('click', ...); }, false); 3. That event handler constructs the required URL either by extracting the ID from the event object (say via the target attribute). 4. It then creates an XMLHTTPRequest for that resource whose 'load' handler will do whatever you want with the result. It's all a bit vague because there are so many ways to handle the details and very little in your question to guide an answer. -- Ben.