PHP Developers Network

JS/jQuery critique
Page 1 of 1

Author:  thinsoldier [ Tue Jun 25, 2013 1:50 pm ]
Post subject:  JS/jQuery critique

Hello I just a built a small tool for myself but I really think the JS/jQuery could be a lot better.
A lot more object oriented I guess.
The script is toward the bottom of the file.

It simply observes a list of radio buttons and a text field. If a radio is chosen and the field contains all integers it makes an ajax request and updates the contents of a div.

Then further down it watches a select field and updates another select field.

Any suggestions?

Syntax: [ Download ] [ Hide ]
<!DOCTYPE html>
<script src="//"></script>
<div id="fromlist">
        <input type="radio" id="from0" value="download" name="from_table">
        <label for="from0">download</label> </li>
        <input type="radio" id="from1" value="competition_policy" name="from_table">
        <label for="from1">competition_policy</label> </li>
        <input type="radio" id="from2" value="licencing" name="from_table">
        <label for="from2">licencing</label> </li>
        <input type="radio" id="from3" value="news" name="from_table">
        <label for="from3">news</label> </li>
        <input type="radio" id="from4" value="decisions" name="from_table">
        <label for="from4">decisions</label> </li>

id# <input type="text" name="from_id" id="from_id_field">

<div id="preview">



<select id="tolist" name="to_table">
        <option value="">Select</option>
        <option value="download">download</option>
        <option value="competition_policy">competition_policy</option>
        <option value="licencing">licencing</option>
        <option value="news">news</option>
        <option value="decisions">decisions</option>

Category <select name="category[]" id="cat"></select>

<input type="submit">



var from_table;
var from_id;
var to_table;

function watchToList(event)
        to_table =;
        // returns long string of option tags <option value="34">foo</option>
        $.get('_section-api.php',{'cmd':'categories','table':to_table })
                .done( function(data) { $('#cat').html(data); });

function watchFromList(event)
                from_table =;

function watchFromID(event)
        var test =^\d{1,}$/)

                from_id =;
        } else {
                console.log('id test fail');

function ajaxForFrom()
        if( from_table && from_id )
                // sample response: {"id":"196","category":"Revoked","headline":"Holdings Revocation Notice"}
                $.getJSON('_section-api.php',{'cmd':'preview','table':from_table, 'id':from_id })
                        .done(function(data) { updatePreview(data) })
        else {
                console.log('not enough info to pull FROM item details')

function updatePreview( jsn )
        var string = '';
        for( i in jsn )
                 string += i + ': ' + jsn[i] + "<br>";



Author:  pickle [ Tue Jun 25, 2013 5:51 pm ]
Post subject:  Re: JS/jQuery critique

There's nothing really wrong with the way you're doing it as far as I can see. Generally though, accepted practice is to use the .on() handler. And since your functions are so short, I'd simply include them as an anonymous function in your call to .on()...

Syntax: [ Download ] [ Hide ]
   //code you currently have in watchToList()
  //"this" refers to "" - could simplify the code a bit

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group