Jump to content

wrapping a textarea value into a jQuery object


skaterdav85

Recommended Posts

basically this small project is for a teacher i work for to copy a bunch of html from one page, put it in this textarea, and i will parse through it and generate new html in a different format. i want to wrap this html into a jquery object so that i can start parsing through it. i only know how to do this from the dom though and not from a textarea's value.i got the value just by doing $('#textarea-id').val();

Link to comment
Share on other sites

What sort of "jQuery object" are you thinking about? If you just want to parse the HTML using JS, there are some libraries available, such as this one.

Link to comment
Share on other sites

well when u do something like $('#someID'), that finds the element with an ID of 'someID' and wraps it into a jQuery object. What i wanted to do was take the value of a textarea (which is a block of html), and find certain values within certain tags using jQuery methods. To use jQuery methods, I have to use a jQuery object.I couldn't figure it out so instead, I took the value from the textarea, appended it to some div so that the html block was in the dom, and then I parsed through the html in that div to build a variable that i wanted. Once I finished parsing and building my variable of new html, I then removed all that html from the div. I'm not sure if this was the most efficient solution, but it worked.

Link to comment
Share on other sites

I was under the impression that

$('#someID')

was equivalent to

document.getElementById('someID')

I think that $('#someID') is just jQuery's way of implementing getElementById()

Link to comment
Share on other sites

I was under the impression that
$('#someID')

was equivalent to

document.getElementById('someID')

I think that $('#someID') is just jQuery's way of implementing getElementById()

They are sort of equivalent. They both fetch the element, but the jQuery implementation wraps the DOM element inside a jQuery object. If you try using regular JS methods/properties on $('#someID') like $('#someID').innerHTML, that will throw an error. You have to use jQuery's implementation like $('#someID').html().
Did you try creating an element without appending it to the DOM? I'm not sure, but you might to be able to use your same parsing techniques on an element that has not been appended.
I did try creating the element via jQuery by passing the variable containing all the HTML from the textarea to the jQuery wrapper $(), but that didn't work. $(textareasHTML).filter() etcMaybe I did create the element from the value in my textarea with $(textareasHTML), but I could have been doing something else wrong. I was on a tight deadline (due today) so I just did it the inefficient way by appending it to the DOM.
Link to comment
Share on other sites

right, but the point I was trying to make was that they are essential the same thing for their respective domains. The idea behind jQuery, from my understanding, is that it is an API/Library for interfacing the javascript language, making common practices easier. (this includes the shorthanded notation of jquery).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...