2

I'm new in Jquery, and I have this code:

jQuery(document).ready(function(){
        var title = jQuery(".fullVisaImg").attr("inselect");

        var links = jQuery(".ff_elem>option");

        for(var i=0; i<links.length; i++) {
            if (title == links[i].value) {
                links[i].attr("selected", "selected"); // here is my problem
                alert(links[i].value);
                return;
            }
        }
    });

i have a select element on my pages, and want to make one of elements selected. if I comment line with // here... all works good, and i see all my option values.

Thanks for help!

7
  • If you just need 1 element selected then why are you looping over all the options and marking them as selected. Why not use html attribute selected in your markup? Commented Dec 18, 2013 at 13:51
  • 6
    i hope it's not because of missing closing quote; anyway, why not use .val()? Commented Dec 18, 2013 at 13:52
  • this select element making with php, i can't change it. Commented Dec 18, 2013 at 13:52
  • 2
    links.eq(i).prop("selected", true); ??? Commented Dec 18, 2013 at 13:53
  • links.eq(i).attr("selected", "selected"); Thanks A. Wolff ! Commented Dec 18, 2013 at 13:57

7 Answers 7

2

When you use [] to access an element in a jquery set, you get back the raw DOM element. So you can not use jquery methods on it directly..

You should also use .prop instead of .attr() when interacting with properties of the element

So use

links.eq(i).prop("selected", true);
Sign up to request clarification or add additional context in comments.

Comments

2

replace you for loop with:

jQuery(".ff_elem").val(title);

I have created this DEMO for you. Check it out.

Although You can iterate through all your option elements and find your option element, and then do this:

links[i].prop("selected", true);

but there is no need to iterate when you can simply let your select element do this for you as I have mentioned above.

Comments

1

This is actually how you can select an option based on the value your options have.

$('select').val('value of the option you want to select');

so use

$(".ff_elem").val(title);

3 Comments

He's looking for a working solution to links[i].attr("selected", "selected");
Why do you keep editing it and breaking the correct way to mark up code?
@epascarello Sorry, i didn't even get why you would add 4 spaces to my answer, probably because i forgot them...
0

Following your code, you could use:

links.eq(i).prop("selected", true);

2 Comments

My guess is you have zero explanation about the reason for the error unlike the one right below that explained the error.
@epascarello ya, sorry about that!
0

if your jquery version is above 1.6+ then use this

links.eq(i).prop("selected", true);

else

   links.eq(i).attr("selected", "selected");

Comments

0

It can be much simpler. Try something like this:

jQuery(document).ready(function(){
    var title = jQuery(".fullVisaImg").attr("inselect");

    jQuery(".ff_elem>option[value=" + title + "]").attr("selected", "selected");
});

3 Comments

Should be prop() with modern versions of jQuery
This is far from simple, as it can be done simpler with jQuery.
It is still simpler than running a for loop to find 'title'.
-2

links is a jQuery collection. When you loop through it, you're just getting the raw element, not a jQuery wrapped version, so you can't use .attr().

Use this instead at your problem line.

$(links[i]).attr("selected", "selected");

Comments

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.