Skip to content

(jQuery plugin) Injecting jQuery elements to context by attrubutes names

License

Notifications You must be signed in to change notification settings

DeivAstra/injextor.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 

Repository files navigation

injextor.js (jQuery plugin)

Inject jQuery elements to context by attrubutes names.

IDEA

We have some container with some included elements:

<table id="container">
    <tr>
        <td><input name="foo"/></td>
    </tr>
    <tr>
        <td><select name="bar"/></td>
    </tr>
    <tr>
        <td><span name="sun"/></td>
    </tr>
</table>

Now, we need load all elements (which has, for example, name attribute) as jQuery elements to context object:

var context = {};
$('#container').injext(context, 'name');

After that, context object contains variables referenced to all elements with name attribute:

{
	foo: jQuery element of <input name='"foo"/>,
	bar: jQuery element of <select name='"bar"/>,
	sun: jQuery element of <span name="sun"/>
}

Good, now we can get access to all necessary injected elements:

context.foo.val('Hello');
context.bar.hide();
context.sun.addClass('star');
Applying multiple attributes

You can do:

var context = {};
$('#container').injext(context, 'name').injext(context, 'id').injext(context, 'whatever');

But keep it simple:

$('#container').injext(context, ['name', 'id', 'whatever']);
Applying values

applyValues just calls val() jQuery function on each element recursively if property name is the same.

var context = {};
$('#container').injext(context, 'name');
context.applyValues({
	foo: 'Bye!'
});
Logging

After adding elements to context object then calls console.log():

injext: foo, bar, sun
Show alert dialog while error occurred

Disabled by default.

var showAlertOnError = true;
$('#container').injext(context, 'name', showAlertOnError);

Good luck.

END PROGRAM

About

(jQuery plugin) Injecting jQuery elements to context by attrubutes names

Resources

License

Stars

Watchers

Forks