javascript - Show/hide text by spiting it with html comment in div -


How do I divide some text into the div by using html tags? Let's say that:

  & lt; Div id = "id1" & gt; Some First Text & lt ;! - more - & gt; here. & Lt; / Div & gt; & Lt; An id = id 1 & gt; Show / Hide & lt; / A & gt; & Lt; Div id = "id2" & gt; Some other text & lt ;! - more - & gt; Okay. & Lt; / Div & gt; & Lt; An id = id 1 & gt; Show / Hide & lt; / A & gt;   

I & lt ;! - more - & gt; After a a jquery or javascript wont be used to show or hide text. Thanks

You can try something like this:

given markup

  & lt; Div id = "id1" & gt; Some First Text & lt ;! - more - & gt; here. & Lt; / Div & gt; & Lt; A & gt; Hide & lt; / A & gt; & Lt; Div id = "id2" & gt; Some other text & lt ;! - more - & gt; Okay. & Lt; / Div & gt; & Lt; A & gt; Hide & lt; / A & gt;  

Add the following javascript:

  // On 1 page load, go to wrap the text & lt; Span & gt; $ (Function () {$ ("div"). (Function () {var html = $ (this). Html (); $ (this) .html (html.replace ('& lt;! - more- - & gt; ',' & lt; span class = "hiddenText">, html) + '& lt; / span & gt;';}); // 2. Visibility visibility while clicking the link Link $ ('a') .click (function () {if ($ (this) .html () == 'Hide') {$ (this) .prev ('div'). Children ('. Hiddentext') Hide (); $ (this) .html ('Show');} and {$ (this) .prev ('div'). Children ('hidden'). Show (); $ (this). Html ('hide');}}};});  

Check me for a working example.

UPDATE : Update solution to rely on different CSS class to hide / show text


Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -