2

In my use case I am trying to get the closest paragraph value in JQuery. In html I am iterating in a list of div elements and trying get the paragraph value using class name from any iteration[choosen].

Here is my Html code:

<div class="qanda questiondiv" id="questionarea" name="questionarea">
    <div class="question-inner">
        <div>
            <div id="topic" class="upvote pull-left">
                <a class="upvote"></a>
                <span class="count">3</span>
                <a class="downvote"></a>
            </div>
            <div >
                <div class="qanda-info">
                    <h6><p class="quest_title_val">{{.QuestionTitle}}</p></h6>
                </div>
                <p class="quest_text_val">{{.QuestionText}}</p>
            </div>
        </div >
        <div class="qanda-info">
            <div class="user-info">
                <a href="/userprofile/{{.UserId}}" ><img src="http://tinygraphs.com/spaceinvaders/Dany?theme=frogideas&numcolors=4&size=220&fmt=svg"/>
                </a>
            </div>
            <h6>{{.UserId}}</h6>
            <span class="date alt-font sub">{{.DateCreated}}</span>
            [<a class="edit_question edit_question_button" >edit</a>|<a class="delete_question_button" id="delted" href="/edithowti/">delete</a>]<br>
            <a id="answertext" name ="answertext" type="submit" class="link-text answerbutton">Answer</a>
        </div>
    </div>
</div>

And my JQuery code:

var originalQuestion = "";

//Enables and disables the question elements 
//for all questions
$('.questiondiv').on('click', '.edit_question', function(e) {
    console.log("In submit edit question text area");
    console.log("question edit");
    e.preventDefault(); 
    //title_val = $(this).closest('p').text('#quest_title_val');
    title_val       = $(this).prev('p').find('.quest_title_val').val();
    question_val    = $(this).closest('.quest_text_val').text();
    console.log("question edit logged in 2");
    console.log("Question title value ", title_val);
    console.log("Question value ", question_val);

    newdiv = "<div class='form-input updated_question'><input name='question_title_val' class='question_title_val' type='text' size='80' value='"+title_val+"' /></div> <br /> <div class='form-input'><textarea name='question_text_val' class='question_text_val' rows='8' cols='80'>"+question_val+"</textarea><br /><div class='form-input'><a type='Submit' id='updatequestion'  name='updatequestion' class='btn btn-primary updatequestion'>Update Question</a><a type='Cancel' id='cancelupdatequestion' name='cancelupdatequestion' class='btn btn-primary cancelupdatequestion'>Cancel</a></div></div>";
    $('.edit_question_button').prop( 'disabled' , true );
    oiginalQuestion = $(this).closest('.questiondiv').innerHTML;
    console.log("Cached div original ", originalQuestion)
    $(this).closest('.question-inner').replaceWith(newdiv);
});

I tried next(), prev(), closest(). But no luck. Could someone help me with this? I am fairly new to JQuery.

Dany
  • 2,692
  • 7
  • 44
  • 67
  • 1
    `$(this).closest('.question-inner').find('.quest_text_val').text();` – Arun P Johny Mar 10 '16 at 06:03
  • 1
    `quest_text_val` is not an ancestor of the `edit_question`, so just by using `closest` you can't find it... instead you can find the `quest_text_val` within the same `question-inner` as the clicked `edit_question` – Arun P Johny Mar 10 '16 at 06:05
  • @ArunPJohny - It worked. Thanks. – Dany Mar 10 '16 at 06:08
  • Also I am trying to catch the html content from questiondiv. But oiginalQuestion = $(this).closest('.questiondiv').innerHTML; is not working. I tried different options . It is not working. Could you help me with that. It would be very helpful. – Dany Mar 10 '16 at 06:16
  • 1
    `$(this).closest('.questiondiv').html()` – Arun P Johny Mar 10 '16 at 06:18
  • @ArunPJohny - Could you help me with this JQuery issue which is similar to this one. http://stackoverflow.com/questions/35965155/how-to-get-textbox-value-from-replaced-div-content-using-replacewith . Sorry I am not able to find a solution for that. – Dany Mar 13 '16 at 05:45

1 Answers1

0
$(this).find('.quest_text_val').html()
cresjie
  • 469
  • 2
  • 13