0

I would like to add CSS to an HTML Element dynamically using Prototype JS. I am using below code.

var wrapper_content = $('wrapper').innerHTML;
wrapper_content.img.setStyle({display:'inline'});

But this is not working.

2
  • Wouldn't .innerHTML just return the html inside the wrapper-element (which isn't a HTML-element). Try with console.log($('wrapper')); and see if it returns anything at all. Otherwise, you might have to check your selector. Commented Jul 9, 2016 at 7:44
  • Thanks @NoLifeKing for your reply. I checked console.log($('wrapper')); and I can see the HTML element at console. .innerHTML is returning HTML content of that element. But I would like to apply css on Images of wrapper_content. How can I do that ?? Commented Jul 9, 2016 at 9:02

4 Answers 4

1

In cases like these you can use the invoke() utility method like so

$$('#wrapper img').invoke('setStyle',{'display':'inline'});

http://api.prototypejs.org/language/Enumerable/prototype/invoke/

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

Comments

0

You could change your selector to var wrapper_content = $('wrapper img');

And then you can do like this wrapper_content.each(function(el) { el.setStyle({ 'display': 'inline' }); });

Edit: Changed the syntax to work with Prototype JS instead.

2 Comments

Thanks @NoLifeKing for your reply. You mentioned here jQuery solution but I need Prototype JS solution. Thanks
Almost right. Prototype uses two dollar signs to signal “return a collection of objects” versus the single dollar sign which is simply shorthand for getElementById. Try var wrapper_content = $$('#wrapper img');.
0

Seems to me you are asking about adding a style to images that are children of the element with the id 'wrapper':

$('wrapper').select('img').each(function(name,index) {
  name.style.display = "inline";
});

Comments

0

Ive been using a combination of Geek Num 88 and T Gibbons answers for a few years. its a combination of css selectors and setting the style display value is more obvious.

$$('#wrapper img').each(function(name) {
  name.style.display = "inline";
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.7.3/prototype.js"></script>
<div id="wrapper">
  <img style="display: none;" src="https://cdn0.iconfinder.com/data/icons/free-business-desktop-icons/128/Home.png" />
</div>

Comments

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.