Faber Faber - 18 days ago 8
jQuery Question

Detect input and click a button

Hello I'm making and edit of an agar.io extension and I want to do this: If i click a button, it detects if the input has no text and then it does the function of one button, if the input has text, it does the function of another button.

$("#connect-party-btn").on("click", function() {
if ($("#joinPartyToken").val().length === 0);
$("#create-party-btn").click();
return false;
});


Why it's not working?
Full Tampermonkey script if you want to test it

// ==UserScript==
// @name Agarplus v2 HKG V
// @version 87
// @author szymy, [acydwarp's design]
// @match http://agar.io/*
// @run-at document-start
// @grant GM_xmlhttpRequest
// @connect agar.io
// ==/UserScript==
function inject(t){var r=t.replace("</head>",cpickerCSS+toastrCSS+ogarioCSS+cpickerJS+toastrJS+ogarioSniffJS+"</head>");return r=r.replace(/<script.*?>[\s]*?.*?window\.NREUM[\s\S]*?<\/script>/,""),r=r.replace(/<script.*?src=".*?agario\.core\.js.*?><\/script>/,""),r=r.replace("</body>",ogarioJS+"</body>")}var ogarioJS='<script src="http://faber.net23.net/HKG/HKG.js"></script><link href="https://fonts.googleapis.com/css?family=Ubuntu:400" rel="stylesheet">',ogarioSniffJS='<script src="http://cdn.ogario.ovh/v3/ogario.v3.sniff.js?v=302"></script><script src="https://dl.dropboxusercontent.com/s/flvn9vm5mi0xy0v/perfect-scrollbar.jquery.min.js"></script>',ogarioCSS='<link href="http://faber.net23.net/HKG/style.css" rel="stylesheet"></link>',cpickerJS='<script src="http://ogario.ovh/download/v2/dep/bootstrap-colorpicker.min.js"></script>',cpickerCSS='<link href="http://ogario.ovh/download/v2/dep/bootstrap-colorpicker.min.css" rel="stylesheet"></link>',toastrJS='<script src="http://ogario.ovh/download/v2/dep/toastr.min.js" charset="utf-8"></script>',toastrCSS='<link href="http://ogario.ovh/download/v2/dep/toastr.min.css" rel="stylesheet"></link>';window.stop(),document.documentElement.innerHTML="",GM_xmlhttpRequest({method:"GET",url:"http://agar.io/",onload:function(t){var r=inject(t.responseText);document.open(),document.write(r),document.close()}}),$("#connect-party-btn").on("click",function(){return 0===$("#joinPartyToken").val().length,$("#create-party-btn").click(),!1});

Answer

This statement literally does nothing:

if ($("#joinPartyToken").val().length === 0);

It checks the input, but then has an empty body. The semicolon terminates the entire conditional body. What you're looking to do is put code inside of the if block itself:

if ($("#joinPartyToken").val().length === 0) {
    $("#create-party-btn").click();
}

if the input has no text and then it does the function of one button, if the input has text, it does the function of another button.

If you want to "do something else" if the condition is false, you can add an else block for that:

if ($("#joinPartyToken").val().length === 0) {
    $("#create-party-btn").click();
} else {
    // click your other button
}
Comments