I'm making a program where user can select an element to render and also select CSS styles to add to the element.
The program successfully renders un-altered elements. The difficulty is changing the styles. So far i'm only changning Width and Height. ! I won't use .width()/.height() methods because I want to include units and change others CSS properties.
I get this error: Uncaught TypeError: latestElement.css is not a function
This tells me that .css() doesn't seem to work with jQuery prototype.
HTML:
```
<section id="form">
<!-- select element --->
<label for="container"> Please select type of container to add</label>
<select id= "container">
<option value= "div" > <div> </option>
<option value= "p"> <p> </option>
<option value= "section"> <section> </option>
</select>
<button id="btn" > Create Container </button>
</section>
<!-- Seperate container to hold now elements -->
<div id="layout_demo">
</div>
```
To avoid assigning a new id attribute to every knew element I select them using the list of each element (i.e $('element')[i]). I store that reference in a variable but it breaks .css().
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script>
$(document).ready( function(){
// event-handler to create new element
function createContainer() {
// variables store values from <select> element
var selector = $('select');
var elementType = $('select').val();
// string containing markup for use with .append()
var elementTypeStr = "<" + elementType + "> This is a " + elementType +
"</" + elementType + ">" ;
$('#layout_demo').append( elementTypeStr );
// create jQuery prototype for prototype.css()
i = $( elementType ).length;
latestElement = $(elementType)[i - 1];
latestElement.css( "width", function(){
return width + widthUnit ;
});
} // end createContainer()
// ---------------------------- event handlers ---------------------
$('#btn').click( createContainer );
// button to delete latest element or selected element
}); // end ready
</script>
0, not1size()is deprecated and has actually been removed from jQuery 3. Ensure you're using an up to date version of jQuery.0toi-1.$(containType)[i]is outside the array, so it returns undefined.