Seak
Seak

Reputation: 110

change property disabled in a variable containing html with jquery or js

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

Upvotes: 2

Views: 1031

Answers (2)

blex
blex

Reputation: 25634

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>

Upvotes: 2

Barry Michael Doyle
Barry Michael Doyle

Reputation: 10608

Add the gcf_crud class to the main div.

Upvotes: 1

Related Questions