I have a form with three fields Timeline, from and to date. I need to implement Reset to Defaults functionality on this (Actually I already have a reset condition on the form.) The problem I am facing is :
* Once the user hits Update, I cant reset the form. Prior to clicking update, form resets perfectly fine. but once the user hits update and new page loads, it doesnt have the default(Monthly ins this case) in the timeline section.
Is it doable with javascript?If yes, then how ? :(
>>Actually the timeline dropdown list values are coming from a backend javacode. I havent hardcoded them as HTML <Select> <Option>
But what does the java code send. What your picture shows looks like an HTML SELECT OPTION field, so I'm assuming that is what your java code is sending. Those are the tags that the browsers understand in order to make select lists.
So the code I posted is meant to look for any <OPTION> tag that contains the word "Monthly" in it (it IS case sensitive) and makes that option the selected option. If it worked for you (and I suspect it did) then indeed, the java page IS sending/generating HTML code)
>>Also, will this code, reset the defaults on From and To fields?
No. It will reset ONLY the SELECT list. Based on the ids of those other fields, you would need:
<a href="#" style='text-decoration: none; color:#2525FF;' onClick='$("option:contains(\"Monthly\")").attr("selected","selected"); $("#from_date").val(\"ALPHA\"); $("#to_date").val(\"BETA\"); return false'>Reset To Defaults</a>I used ALPHA and BETA so you can see clearly where you need to specify the default values. If you need the fields empty, then change:\"ALPHA\"to:\"\"
Instead of:onClick="document.forms[0].reset();return false">Reset To Defaultstry:onClick="$("option:contains('Monthly')").attr("selected","selected");return false">Reset To Defaults
If you have more than one timeline select list, then give each list an id - ex::
<select id="select1"> <option value="daily">Daily</option> <option value="weekly">Weekly</option> <option value="monthly">Monthly</option></select><select id="select2"> <option value="daily">Daily</option> <option value="weekly">Weekly</option> <option value="monthly">Monthly</option></select>Then use that id on the onclick event - ex:$("#select1 option:contains('Monthly')").attr("selected","selected");will select Monthly only for select with id="select1"
what you posted has syntax errors. You onclick should start and end with apostrophes, NOT double quotation marks since the value within the onclick already uses double quotation marks. Copy and paste this:
<a href="#" style='text-decoration: none; color:#2525FF;' onClick='$("option:contains('Monthly')").attr("selected","selected");return false'>Reset To Defaults</a>
>> Did it work?
yes Sir, perfectly fine :) Thanks a ton.
>> I havent hardcoded them as HTML <Select> <Option>
I meant, those values are stored in backend bean and fetched on the fly. but yes I saw the generated HTML, its <OPTION> :)
>> $("#from_date").val(\"ALPHA\");
so instead can I supply the variable I am using to store a default value?
>>so instead can I supply the variable I am using to store a default value?
try it! You learn better by making mistakes and banging your head against the wall :)
>>Actually the timeline dropdown list values are coming from a backend javacode. I havent hardcoded them as HTML <Select> <Option>
But what does the java code send. What your picture shows looks like an HTML SELECT OPTION field, so I'm assuming that is what your java code is sending. Those are the tags that the browsers understand in order to make select lists.
So the code I posted is meant to look for any <OPTION> tag that contains the word "Monthly" in it (it IS case sensitive) and makes that option the selected option. If it worked for you (and I suspect it did) then indeed, the java page IS sending/generating HTML code)
>>Also, will this code, reset the defaults on From and To fields?
No. It will reset ONLY the SELECT list. Based on the ids of those other fields, you would need:
Open in new window