1

I am getting a little frustrated and I know I have probably ballsed my code up but here goes.

I have a select dropdown that populates it's options with user id's, the select dropdown has a default value which I am trying to not use in the query.

I then want a chosen select option to populate a hidden text input with the selected user id from the option value.

So the code is detecting a change on the select, then it is saying if not default then copy value (user id) into the desired text input.

Here is my messy attempt at the jquery code, sorry for probably making a lot of mistakes with it.

jQuery(document).ready(function($){
    $('.client-select').on('change',function(event){
        if $('.client-select').val($(this) =! "default"){
            $('.clientuserid').val($(this).find('option:selected').value());
        }
    });
});

Any help with this would be greatly appreciated.

Many thanks.

Charlie

2 Answers 2

2

Try below updated javascript code of yours

$(document).ready(function(){
    $('.client-select').on('change',function(){
         if($(this).val() != "default"){
            $('.clientuserid').val($(this).val());
        }else{
            $('.clientuserid').val('');
        }
    });
});

Also here is the working demo fiddle - http://jsfiddle.net/1d9hfrxp/1/ I hope this is what you were looking to achieve.

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

2 Comments

Hi @Charlie , glad to solve your problem. If you are satisfied with the solution can you mark it as the answer and probably upvote it, so that it can be helpful to others who might come across issue similiar to yours
Hi I think I ticked the answer and upvoted it. Let me know if I haven't and thanks again :-)
0

You have an error on your jquery formating script. You missed () for if function. Please try:

jQuery(document).ready(function($){
    $('.client-select').on('change',function(event){
        if ( $('.client-select').val($(this)) =! "default"){
            $('.clientuserid').val($(this).find('option:selected').val());
        }
    });
});

val() return the value of your dropdown selected element

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.