2

I'm creating custom validation function used in Magento and there are passed 2 parameters to callback: v - value of field, element - element that is validated. My problem is that this HTML element is string and I can't use Prototype to create JavaScript object like when I use jQuery:

var element = '<input type="text" value="ABC" name="some_name" class="class1 class2" />';
console.log(jQuery(element));

How to get the same result with Prototype?

1 Answer 1

3

If you have a string that is HTML and want a HTMLElement object from it this will work

var element = '<input type="text" value="ABC" name="some_name" class="class1 class2" />';
var $element = new Element('div').update(element).down('input');
//change the down() method to the appropriate CSS selector

This is also helpful if you need to select specific elements out of a long string of HTML

var element = '<div id="div1"><input type="text" value="ABC" name="some_name" class="class1 class2" /></div><div id="div2"><span>Valuable text</span></div>';
var $element = new Element('div').update(element).down('div2 span');
Sign up to request clarification or add additional context in comments.

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.