1

I have code (edited, after Brad Christie suggestions):

drupal_add_js('

jQuery(document).ready(function(){ 

jQuery("#selFinishes option").each(function(index) {
if (jQuery(this).val() == ' . $filter_color . ') {
jQuery(this).attr("selected","selected")
}   
})

});
', "inline");

And it sucessfully adds "selected" attribute ($filter_color is added via PHP) to the selected value. But when i target multiple select fields like this:

   drupal_add_js('

jQuery(document).ready(function(){ 

jQuery("#selFinishes option").each(function(index) {
if (jQuery(this).val() == ' . $filter_color . ') {
jQuery(this).attr("selected","selected")
}   
})

 jQuery("#selThemes option").each(function(index) {
if (jQuery(this).val() == ' . $filter_theme . ') {
jQuery(this).attr("selected","selected")
}   
})

});
', "inline");

Both of loops fail to work!

Thanks for tips!

2 Answers 2

2

The code above is apparently mixed javascript and php, but it not possible to tell what might be wrong with it. You should check (by viewing source in the browser) whether the resulting javascript code is what you intended. If possible post the resulting javascript here.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh.. i had emtpy values because in some occasion value was not present... i added whole loop under isset to check first if variable exists! Thanx
1

My educated guess is that the resulting JavaScript is invalid when it's output. Guessing by the variable names ($filter_color & $filter_theme) one (or both) of them is/are most likely a string making the test if (...val() == string) fail which in terns make the entire block of JavaScript result in a syntax error and fail entirely.

Try changing your output to encase the value in a string (maybe something like the following):

[snip].val() == "' . $filter_theme . '") {[/snip]

Notice I added double quotes on either side of the variable before and after the PHP concatenation. This should fix the syntax would should intern make it work again.

Again, too little information in your question but this would be my guess at the solution

2 Comments

Hey Brad, sorry, now i edited the question to show whole code, Javascript is added inline by Drupal. When one loop is present, things work fine, when both, both fail to work...
Brad, it was problem with having not resulting php value present! Sorry for misleading you and thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.