user3733831 user3733831 - 3 months ago 7
jQuery Question

How can I fix this jquery issue?

I do have a

input['text']
field to insert date and also have a
dropdown
for select category.

When changing the date input field I want to make the page reload with attaching date and category values in the URL.

NOTE: Here I want to attach category value if it is not empty.

This is how I tried it. But it doesn't work for me.

$("#date").change(function() {
var c = $("#category").val();
var d = $(this).val();
if(c.length === 0 ) {
var cat = "$cat="+c;
} else {
var cat;
}

location.href="?p=attendance&todate="+d.add(cat);
})


This is the output, when category ID is not empty.

?p=attendance&todate=2016-08-04undefined


Can anybody help me to fix my problem?
Thank you.

Prs Prs
Answer

You could try this approach. Put only what you have.

$("#date").change(function() { 
    var url = ''; 
    var c = $("#category").val(); 
    var d = $(this).val(); 
    if(c.trim().length) { url+='&cat='+c } 
    if(d.trim().length) { url+='&todate='+d } 
    location.href="?p=attendance"+url; 
})
Comments