[solved] getElements and inject question

here you will find only solved 1.1 Help topics

Moderator: 1.1 Moderators

[solved] getElements and inject question

Postby keiron77 on Wed Feb 03, 2010 1:02 pm

I have only just started looking at Mootools(v 1.11) to solve a particular problem that needs to be done in this framework rather than my normal approach in JQuery. I need to insert some html after an element that I can only identify by name (not id).

In the head I have:

  1. <script src="media/script/mootools/mootools.js" type="text/javascript"></script>


Html element:

  1. <div id="tabGeneral">
  2. <input name="ta" type="text"/>
  3. </div>


My JS script (after the html element)

  1. <script type="text/javascript">
  2. $('tabGeneral').getElements('input[name=ta]').inject("<strong>Hello World!</strong>","after");
  3. </script>


I have tried several approaches and nothing seems to work.
keiron77
 
Posts: 3
Joined: Wed Feb 03, 2010 12:53 pm

Re: getElements and inject question

Postby daKmoR on Wed Feb 03, 2010 1:10 pm

getElements with give you back an array, so you need to take the first one or use getElement; inject needs an element
  1. $('tabGeneral').getElements('input[name=ta]')[0].inject(new Element('strong', { html: "Hello World!"}),"after");
"Speak when you are angry and you will make the best speech you will ever regret." - Ambrose Bierce
User avatar
daKmoR
Site Admin
 
Posts: 860
Joined: Mon Jul 14, 2008 11:40 am

Re: getElements and inject question

Postby keiron77 on Wed Feb 03, 2010 2:27 pm

That isn't working for me. When the page loads Firebug says:

C.parentNode is null
break;case"after":var B=C.getNext();if...stChild;if(D){C.insertBefore(this,D);
(mootools.js (line 63))


In addition to that I see adding html isn't quite as simple as I was thinking, to add a button with a JS function would that be:

  1. $('tabGeneral').getElements('input[name=ta]')[0].inject(new Element('input', { type: "button", value: "Insert", onclick: "BrowseFileServer('ta')"}),"after");
keiron77
 
Posts: 3
Joined: Wed Feb 03, 2010 12:53 pm

Re: getElements and inject question

Postby daKmoR on Wed Feb 03, 2010 7:31 pm

oh...

mootools 1.11

and yeah I forgot to replace inject with grab... but yeah mootools 1.11...

  1. new Element('strong').setText("Hello World!").injectAfter( $('tabGeneral').getElements('input[name=ta]')[0]);

http://jsfiddle.net/wZQxz/
"Speak when you are angry and you will make the best speech you will ever regret." - Ambrose Bierce
User avatar
daKmoR
Site Admin
 
Posts: 860
Joined: Mon Jul 14, 2008 11:40 am

Re: getElements and inject question

Postby keiron77 on Wed Feb 03, 2010 8:26 pm

Thanks daKmoR.

Using the snippet of code you wrote, plus your link to jsfiddle.net and I bit of research I got it all working:

  1. new Element('input', {type:'button',value:'Insert', id:'tainsert', onclick:'BrowseFileServer('ta')'}).injectAfter( $('tabGeneral').getElements('input[name=ta]')[0]);
keiron77
 
Posts: 3
Joined: Wed Feb 03, 2010 12:53 pm


Return to Solutions

Who is online

Users browsing this forum: No registered users and 1 guest