0

I'm trying to get an id field by concatenating a sting and an input value. The input value changes based on a previous action in the page and I want to be able to use that create an action.

            var ID=$("#PeoplePickerIDField").val();
            var blocker="$('#block"+ID+"')";
            if (blocker.val() == "Allow"){
                alert("Allowed");
            } else {
                alert("Not Allowed");
            }

The value of the field "PeoplePickerIDField" is the one that changes and this corresponds to the block field also (this id already changes based on what is in the "PeoplePickerIDField").

any help would be appreciated :)

thanks

1
  • blocker=$('#block"+ID), Just create a valid selector using string concatenation the use $() Commented Apr 26, 2016 at 12:37

2 Answers 2

3

Try:

            var ID=$("#PeoplePickerIDField").val();
            var blocker=$("#block"+ID); 
           if ($(blocker).val() == "Allow"){
             alert("Allowed");
           } else {
            alert("Not Allowed");
            }
Sign up to request clarification or add additional context in comments.

1 Comment

That's spot on thank you, I was clearly overdoing the markup lol. once the 10 minute accept countdown will allow me I'll accept this as the correct answer :) Thanks again.
1

jQuery is powerful library.You can do same by matching ID attribute.

$(function(){
var blocker=$('[id=block'+ $("#textId").val() + ']');
            if (blocker.val() == "Allow"){
                alert("Allowed");
            } else {
                alert("Not Allowed");
            }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='text' id='textId' value='test'/>
<input type='text' id='blocktest' value=''/>

5 Comments

that's very nice indeed @A.T. would save me using a var that's not needed. will keep that one in my back pocket for later :) thanks
when OP is using IDs selector, suggesting attribute value selector is slow and incorrect as identifiers are unique
@Satpal you are right but sometimes you need to do things that way.
@A.T., When?? as duplicate identifiers renders your document invalid
@Satpal i am not saying id is duplicate, kindly recheck my answer. In case, if you need to find element with id ending with some string e.g. RANDOM_NUMBER_CRAP_ID. if you have web form experience you will see long ids of html elements. for more see api.jquery.com/attribute-contains-selector that's why such things exist

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.