0

I have a JSF page where I'm trying to tie the text in specific paragraphs to the contents of a set of textareas.

Getting the content to change when a textarea changes is dirt simple using onchange and onkeyup events:

onchange="$('dynamicParagraphId').text($(this).val());"

Unfortunately, I'm having some trouble initializing the paragraphs so that their text matches the textareas when the page is initially loaded.

Because of how the page is implemented, editing the underlying HTML is bloody difficult; I'm not sure how to implement an obvious solution like a script that triggers when the page loads, because it's going to take some real work for me to get a hold of the textareas' IDs. Is there some way to insert Javascript/jQuery code into the textarea definition that will trigger when the page loads so that I can make use of the this object and not have to figure out the textarea ID? Is there some feature of jQuery I can leverage that spares me needing to know the IDs?

8
  • 2
    Nope. You have to know something about the textarea to select it, such as it's parent, a sibling, it's id or class, any of it's attributes that are unique to the textarea, etc. Commented May 20, 2013 at 19:26
  • Did you try $('textarea')? You will then have all textareas on the page and can go from there... Commented May 20, 2013 at 19:28
  • can you throw up a brief sample for us to look at? Commented May 20, 2013 at 19:28
  • You are binding to these events directly from the markup? Use the same function in 'onload'. Commented May 20, 2013 at 19:29
  • 1
    @Malk textareas don't have an onload event. Commented May 20, 2013 at 19:31

2 Answers 2

1

Trigger the keyup event for all textareas, but you can probably come up with a more specific selector:

$(document).ready(function(){
  $("textarea").keyup();
});

Or if you can only attach functions through inline markup for some reason you can add it to the <body>:

<body onload='$("textarea").keyup()'>
Sign up to request clarification or add additional context in comments.

Comments

0

If you know how many textareas there are and which you want (i.e. the 5th on the page), you could easily query for all textareas:

document.getElementsByTagName("textarea");

and select the one you want. Another solution is to hand over the control to JS and render the textarea on the clientside. http://jsfiddle.net/FExQy/

2 Comments

Okay, so I CAN give all the textareas I want to behave this way the same class. But then the problem becomes, how do I find the ID of the paragraph I want that textarea to be tied to? Is there some way to wildcard the class -- ie, give each textarea a class of "tied-to-paragraph-[PARAGRAPH_ID]", and then get every textarea with a class that begins "tied-to-paragraph-"?
No. I'd set a data-attribute and pull from there <textarea data-paragraph="paragraph"></textarea> then you can use $('textarea[data-paragraph]') to get all the textareas that are linked to paragraphs and .data('paragraph') to get the value stored for a specific one

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.