0

I tried with this code but didn't work with me:

var data = document.querySelector;
$( document ) .querySelector('.class2').textContent() {
$('.class1').val('.class2');
});

The whole idea is that I want to get data from class2 and replace them in class1 and collect them with the Form inputs.

Thank you!

7
  • 2
    Looks like you're mixing jquery $() with javascript .querySelector. And do you want data-? or the value? or the text? In this case creating including HTML may help, along with expected input/output values. See minimal reproducible example. Commented May 25, 2020 at 16:05
  • Your code looks like it should be $(".class1").val($(".class2").text()) but without HTML/expected values, hard to tell. Commented May 25, 2020 at 16:07
  • Hi @freedomn-m, I use this input code: {% for line_item in cart.items %} <input type="hidden" name="entry.2105509418" id="class1" value=""> <span class="class2">{{line_item.product.title}}, {{line_item.quantity}} - </span> {% endfor %} , I want to det data from SPAN class2 and collect them in value of input class1 , any recommendations to make it works even with Javascript or jQuery? thanks in advance Commented May 25, 2020 at 16:39
  • 1
    id="class1" / class="class2" - that's pretty confusing to use "class1" in an id=. You can edit your question and add that info rather than in a comment. Also if you have a for loop, then don't use id= inside it because IDs must be unique with the document. Give your hidden input a class. Commented May 25, 2020 at 16:48
  • Next: how are you triggering your code? Is there a button in each of your cart.items? Commented May 25, 2020 at 16:48

1 Answer 1

3

You have to select the class2 element and get its value before setting it to the first one. Also, if you have only one item with class1 or class2, use id's instead.

var data = $('#class2').text();
$('#class1').val(data);

Hope this helps.

Update:

Assuming what you want is to show an alert if the string is empty and only transfer it it's not, you will have to write your code this way:

if(field1 == "") {
   alert('');
} else {
   var data = $('#class2').text();
   $('#class1').val(data);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Hi @Romi Halasz, thanks for your answer I think it will work but I don't know I can put your code in this code to collect data in field1: if(field1 == ""){ alert(''); var data = $('#class2').text(); $('#class1').val(data); } Is the way that I've used to put your code right? If not can you help me to write it in the right way, thank you so much for your help, I realy appreciate it
Hi James, I have updated my answer, but it's based on my assumption of what your comment means. Let me know if that works for you.
Hi Romi, unfortunately it's not working by I think your code is right but I'm doing something wrong, so to be in the picture, I use this input code: {% for line_item in cart.items %} <input type="hidden" name="entry.2105509418" id="class1" value=""> <span class="class2">{{line_item.product.title}}, {{line_item.quantity}} - </span> {% endfor %} And I use your code like this: if(field1 == "") { alert(''); } else { var data = $('.productdata').text(); $('#pdata').val(data); } Any recommendations to make it works?
Well, that is out of the scope of your initial question. You have not mentioned that you use a for loop, and regarding the code in the comment, who is .productdata and #pdata ?
I've edited it and gave the hidden input a class and here's the new code: {% for line_item in cart.items %} <input type="hidden" name="entry.2105509418" class="class1" value="{{line_item.product.title}}, {{line_item.quantity}} -"> <span class="class2">{{line_item.product.title}}, {{line_item.quantity}} - </span> {% endfor %} , now how I can get data from class2 and collect them in value in input class1?
|

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.