0

I have a drop down list [ Incident, Question, Problem, Task]. I have written a code when an end user logins in and has a tag called product the default value should be problem. However it does not seem to work. It still gives the user the option to select values from the list.

$j(document).ready(function() {  
if(location.pathname == '/requests/new') {
var ct = currentUser.tags;

 if(ct.indexOf("product") >= 0){
 $j(document.getElementById("ticket_fields_75389").value = "Problem"); 
  }
 else {
 $j(document.getElementById("ticket_fields_75389").value = "");
 } 

 }
 })
Renato Lyke
  • 43
  • 10

1 Answers1

0

Note: This answer is incorrect. I will delete it once I know the OP read my comment.


It still gives the user the option to select values from the list.

Well, of course, setting value will only change the initially selected value. If you want "fix" the value of the select element, so that the user cannot change it, you have to make it read-only. Lets stick with jQuery (what you have is a weird mix of DOM API and jQuery ):

$j("#ticket_fields_75389").val("Problem").prop('readonly', true);

†: Let's have a close look at the line

$j(document.getElementById("ticket_fields_75389").value = "Problem");

What exactly is happening here? Obviously we have a function call ($j(...)) and we pass something to it. This "something" is the result of the expression

document.getElementById("ticket_fields_75389").value = "Problem"

This finds an element by ID and assigns the string "Problem" to the value property. This is an assignment expression. The result of an assignment expression is the assigned value, i.e. "Problem".

That is the value that is passed to $j(...), so we have $j("Problem");. Since $j refers to jQuery, this would search for all elements with tag name Problem, which does not exist in HTML. It would also return a jQuery object, but you are not doing anything with it.

Hence the wrapping in $j(...) is completely unnecessary or even wrong, even though it doesn't throw a syntax or runtime error.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Hi the reason i used this code is because i am wrting an API in Zendesk. This worked for another drop down list. Here it is not working which is quite strange. I tried the suggestion it is not working. – Renato Lyke Oct 03 '13 at 07:26
  • Not working how? I can only help you if you describe your problem and your goal precisely. – Felix Kling Oct 03 '13 at 07:58
  • Assume that you are an end-user having some kind of problem with a item. We have multiple forms that you can raise a concern. E.g Sales. Since your id is associated with a tag called product and you select the sales form. There are various ticket fields associated with the form. Once such field is Type. Which has the drop down list as mentioned. Since you have a tag called product the default option should be "problem". – Renato Lyke Oct 03 '13 at 10:37
  • OK. But you still didn't explain what exactly didn't work. In any case, I noticed that `readonly` has no effect on `select` elements, so that might be what you are referring to. I found this question which explains what to do: http://stackoverflow.com/questions/368813/readonly-select-tag. I will leave this answer until you read my comment (pls let me know) and then delete it because it clearly doesn't solve the problem. – Felix Kling Oct 03 '13 at 14:08