0

I am trying to write some javascript that will return the value of toasty.png and bready.png when the user clicks on the respective text. I'm able to return "Toast" and "bread" but not the other text. Any advice?

<script>
    $(document).on('vclick', '.changePageButton', function() {
        console.log(this.text);
        //console.log(value within the image)
    });
</script>

<a class="changePageButton" value="Toast" data-transition="slide">
    <input type = "hidden" name = "image" value = "toasty.png">
    <input type = "hidden" name = "video" value = "video1.mpg">
    test
</a>

<a class="changePageButton" value="bread" data-transition="slide">
    <input type = "hidden" name = "image" value = "bready.png">
    <input type = "hidden" name = "video" value = "video2.mpg">
    test
</a>
0

3 Answers 3

2
// Also did you mean "click"?
$(document).on('click', '.changePageButton', function () {
    var inputs = {};

    console.log(this.text);

    $(this).children('input').each(function (v) {
        inputs[$(this).prop('name')] = $(this).val();
    });

    console.log(inputs);
    console.log(inputs.image);
    console.log(inputs.video);
});
Sign up to request clarification or add additional context in comments.

8 Comments

What if I have multiple hidden input fields within the <a> tag?
Then you'd pick one of them using .get(x) or you'd be more specific in the .children() parameter.
I just edited my code above. How would I be more specific using .children?
Which do you want returned? All of them?
Yes, separately though.
|
0

try this

$(document).on('vclick','.changePageButton', function() {

     console.log($(this).find("input[type='hidden']").val());

     // if you want according to hidden field name
     console.log($(this).find("input[name='image']").val());

});

I hope it will help

2 Comments

What if I just want the value within the hidden field with name "image"?
you can use find("input[name='image']")
0

Form tag usable for get element

<script>
 $(document).on('vclick','.changePageButton', function() {
  var frm = document.getElementById('ID');
  // jQuery frm = $("#ID")
  console.log(this.text);
  console.log(frm.image.value[0]);
  console.log(frm.image.value[1]);
  // or you can use loop  FOR, WHILE etc
 });
</script>
<form id="ID">
    <a class = "changePageButton" value = "Toast" data-transition="slide">
        <input type = "hidden" name = "image" value = "toasty.png">
        test
    </a>

    <a class = "changePageButton" value = "bread" data-transition="slide">
        <input type = "hidden" name = "image" value = "bready.png">
        test
    </a>
 </form>

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.