6

I am building up a multi-step questionnaire. I have 3 questions (3 divs), the DOM looks like this(Pseudo-code). My question are

1.How can I read the value from the field which type is url ( type='url') in q3?

2.What do read only the non-empty text/textarea/url fields? Meaning I only want to read the text fields when user typed something into it.

I was thinking a stupid way to read each fields no matter if it empty. then i have isset/empty php command to see if this is empty, if so, then i will not take a value. But is there any better way to achieve this?

 <div id=q1>
   <input type='text' id='q1text'>
   <input type='button'>   // this btn will hide q1 and show q2.
 </div>

 <div id=q2 style="display:none">
   <input type='textarea' id='q2textarea'>
   <input type='button'>  // this btn will hide q2 and show q3
 </div> 

 <div id=q3 style="display:none">
   <input type='url' id='q3url'>    // this btn will submit the form data.
   <input type='submit'>
 </div>

1 Answer 1

6

1.How can I read the value from the field which type is url ( type='url') in q3?

It has id attribute. You can use id selector along with .val()

$('#q3url').val();

2.What do read only the non-empty text/textarea/url fields? Meaning I only want to read the text fields when user typed something into it.

You can use filter function to filter out the element that do not have value in them:

var allnonemptyurls = $('input[type="url"]').filter(function () {
  return !!this.value;
})
Sign up to request clarification or add additional context in comments.

4 Comments

Good answer. Nice use of double not to convert the value to a boolean.
@MilindAnantwar Thanks for the quick reply. I just try your method. I found out this.. the var allnonemptyurls is not a boolean when I try look it up on console. However i put a break point inside the filter callback. !!this.value did return me a boolean, I dont know for some reason when we return the !!this.value back to allnonemptyurls, it actually becomes a DOM object for that url element. Any idea?
That is what it does. it gets the filtered dom object for non empty URLs. what exactly you want??
I guess i understand what you are saying now. I will take the filtered DOM object and if the DOM is empty that means users did not type in anything in that DOM right?

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.