0

Below is sample jQuery code

$('#HomeCity option:selected').text()

how do change HomeCity to optionElement[key] which from foreach function?

I did

$(" '#' + optionElement[key] option:selected ").text()

but not working.

3 Answers 3

1

You should quotes properly to create correct selector.

Use

 $("#" + optionElement[key] + " option:selected ").text()
Sign up to request clarification or add additional context in comments.

Comments

1

make it

$( '#' + optionElement[key] + " option:selected ").text();

basically after optionElement[key] you need to start the quotes " again and before '#' no double quotes are required.

Comments

1

Currently the code you mentioned wont be working because you have wraped it inside double quotes. So as per your code

$(" '#' + optionElement[key] option:selected ").text()

'#' + optionElement[key] option:selected is one string that jQuery is searching for and it doesnt exist in your page.

What you should be doing to make it dynamic is

$("#"+optionElement[key]+"option:selected").text()

This way your optionElement[key] is dynamic which will be changed as per the option selected.

Hope this be of some help.

Happy Learning

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.