views:

151

answers:

3

I've got a form with various inputs that by default have no value. When a user changes one or more of the inputs all values including the blank ones are used in the URL GET string when submitted.

So to clean it up I've got some javascript that removes the inputs before submission. It works well enough but I was wondering how to put this in a js function or tidy it up. Seems a bit messy to have it all clumped in to an onclick. Plus i'm going to be adding more so there will be quite a few.

Here's the relevant code. There are 3 seperate lines for 3 seperate inputs. The first part of the line has a value that refers to the inputs ID ("mf","cf","bf","pf") and the second part of the line refers to the parent div ("dmf","dcf", etc).

The first part is an example of the input structure...

echo "<div id='dmf'><select id='mf' name='mFilter'>";

This part is the submit and js...

     echo "<input type='submit' value='Apply' onclick='javascript: if (document.getElementById(\"mf\").value==\"\") { document.getElementById(\"dmf\").innerHTML=\"\"; }
if (document.getElementById(\"cf\").value==\"\") { document.getElementById(\"dcf\").innerHTML=\"\"; }
if (document.getElementById(\"bf\").value==\"\") { document.getElementById(\"dbf\").innerHTML=\"\"; }
if (document.getElementById(\"pf\").value==\"\") { document.getElementById(\"dpf\").innerHTML=\"\"; }
' />";

I have pretty much zero javascript knowledge so help turning this in to a neater function or similar would be much appreciated.

+3  A: 

Seems pretty simple:

<script>
function doSubmit() {
    if (document.getElementById("mf").value == "")
        document.getElementById("dmf").innerHTML = "";
    if (document.getElementById("cf").value == "")
        document.getElementById("dcf").innerHTML = "";
    if (document.getElementById("bf").value == "")
        document.getElementById("dbf").innerHTML = "";
    if (document.getElementById("pf").value == "")
        document.getElementById("dpf").innerHTML = "";
}
</script>
<input type="submit" value="Apply" onclick="doSubmit();" />

Or you could even get fancy and do something like this:

<script>
function doSubmit() {
    var inputs = {
        "mf": "dmf",
        "cf": "dcf",
        "bf": "dbf",
        "pf": "dpf"
    };
    for (var input in inputs) {
        if (document.getElementById(input).value == "")
            document.getElementById(inputs[input]).innerHTML = "";
    }
}
</script>
David Wolever
That's still ugly. You could use a string array and a loop.
SLaks
Sure, that'd work too. I chose this implementation because, in my experience, it's better to be explicit about things like element names (so that, eg, they are searchable and special cases can easily be handled). But implicit works too.
David Wolever
+7  A: 

your script block in your HEAD:

<script type="text/javascript">
    function yourFunctionName(){
       ...your javascript goes here...
    }
</script>

and then your onclick:

onclick="javascript:yourFunctionName()"
DA
+1 answers the question, nothing more.
Anurag
You can leave off the `javascript:` part of the onclick property. That's only necessary if it's the href on a link.
Matthew Crumley
A: 

if you were to use jquery (and there is no reason not to): if your submit button had an id of say id="submit-button" this 'should' work and would handle any additional inputs that get added

$(document).ready(function () {
    $("#submit-button").click(function () {
       $(this).parents("form").find(':input').each(function() {
           if ($(this).val() === "") {
               $(this).innerHTML = "";
           }
       });
    });
}); 

NOTE: I did not test above code at all

here is an updated version

$(document).ready(function () {
    $("form").submit(function () {
       $(this).find(':input').each(function() {               
           if ($(this).val() === "" && $(this).attr("type") !== "submit") {
               $(this).attr('disabled', 'disabled');
           }
       });
    });
}); 

this one is setting the blank inputs to disabled - I could not get the innerHTML = "" to work on Firefox 3.6 on Mac and I did not test above code on other browsers or OS

you would need to download jquery http://docs.jquery.com/Downloading_jQuery#Current_Release and include a reference to it in your html head

to use the innerHTML trick on the inputs parent span tag change the line $(this).attr('disabled', 'disabled'); to

if ($(this).parent().get(0).tagName === "SPAN") {
    $(this).parent().get(0).innerHTML = "";                     
}         
house9
are you saying this would dynamically ignore the zero value inputs on submission?
Taylor
the code finds all inputs in the form that contains the submit button which was clicked and for each input which has a blank value it sets the innerHTML of that input to blank. As long as that innerHTML trick works - then my answer to your question is yes
house9
updated code, see 2nd code block
house9
Will be great if this works.think the innerHTML thing doesn't work because the value is within the tag itself and not considered to be the inner HTML. Thats why I ended up wrapping the inputs with a corresponding div.There are actually some selects as well as inputs if that makes a difference? How about having it remove the innerHTML of a span tag with a child that has a zero value (if that makes sense).html would look something like[SPAN] [input type=... id="a" value=""] [/span][SPAN] [input type=... id="b" value=""] [/span][SPAN] [select type=... id="c"] [/span]
Taylor
how would I use your code in the context of my code. Is there a function to call in onclick=""?
Taylor
no, the document ready bit runs once the page has finished loading and then it automatically binds the function to the form submit event
house9
updated again - added a few lines that handle when using a parent span tagand yes this should handle select menu as well, see http://api.jquery.com/input-selector/
house9
I doff my cap to you sir. Just implemented your code using the 'SPAN' and innerHTML change instead of the disabled part and it works brilliantly. It worked straight away without any head scratching and I'll have more use of jquery on my site anyway so including that isn't a big deal. DA and David answered my question but you've gone the extra step and provided exactly what I was after. thanks!
Taylor
just tried it with the 'disabled' line of code instead of 'innerHTML' trick. That works as well and theres no need for the span tags either. even better!
Taylor