2

I need to disable the elements of class "gcf_crud" that are into a variable.

My wrong code is:

var defText = ''+
'   <div class="form-group col-md-12">'+
'       <h4 id="minimum-setp">{{title}}</h4>'+
'       <input type="text" class="form-control gcf_crud" id="txtUsuari" value="{{data}}"/>'+
'   </div>';
var defTextDisabled = $(defText).find('.gcf_crud').prop('disabled', true);

With this code i'm obtaining only the input but I need all the original html.

How may I do it right?

Regards, Seak

7
  • "i'm obtaining only the <input>". That is because you only select the input: .find('.gcf_crud') Commented Apr 19, 2016 at 20:22
  • and how to select all but only disable the .gcf_crud class elements? Commented Apr 19, 2016 at 20:27
  • 1
    I'm not entirely sure of what you want to do, but is this it? jsfiddle.net/ku2j3c5m Commented Apr 19, 2016 at 20:32
  • yes! That's exactly what i wanted!! wich differences are between your and mine codes? Commented Apr 19, 2016 at 20:36
  • Could you provide a coded example where there is more than one element with the gcf_crud class? Commented Apr 19, 2016 at 20:36

2 Answers 2

2

Your defTextDisabled variable only returns the input because of .find('.gcf_crud').

But it seems that you need a reference (variable) to a jQuery object containing all the elements. In order to do that, break down your process in steps:

var defText = '<div class="form-group col-md-12">'
            +   '<h4 id="minimum-setp">{{title}}</h4>'
            +   '<input type="text" class="form-control gcf_crud" id="txtUsuari" value="{{data}}"/>'
            + '</div>',
    $defText = $(defText); // Save the entire thing here
    
// Now, you can disable the input
$defText.find('.gcf_crud').prop('disabled', true);

// And use the whole content
$('body').append($defText);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>

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

1 Comment

I understand perfectly my mistake. Thanks a lot to all. I'm auto-learner and this tricky habbits are difficult to find in tutorials and googleing.
1

Add the gcf_crud class to the main div.

4 Comments

same behavior. var defTextDisabled is only the input
Sorry, I've changed my answer.
Your solution would disable also the div and i wanted only the elements with class gcf_crud
You're right, my bad I misunderstood the question. Glad you found an answer.

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.